Every content here is my original work.Creative Commons Licence
Universitas Scripta is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Your cookies may be used by Google and mathjax. See Google's privacy policy.

Monday, 22 March 2021

little tips to close overlay on SwiftUI

 This is about the architecture of SwiftUI.

Objective: I want to open and close little overlay message box.

When closed, I don't want to use system resource, and additionally I'll be happy if I can use onAppear and onDisappear.

Also, I want my closing animation work properly.

Implementation:

1. Use complete object destruction by defining 

func View.overlay(_ view: View?) -> View {
    if view == nil{
        self
    }else{
        self.overlay(view)
    }
}

In this way, when I started closing animation by withAnimation{overlayView = nil}, the screen shows preview of the main view, so I see all the blinks and glitches.

2. opacity -> 0

By this method, I am worried that I cannot achieve the objective. It is wasting my resources and I think I cannot use onDisappear.

3. hidden(_ bool: Bool)

Define this function for View. This worked perfectly so I did not tested 2nd option. I think this is supposed to do, designed by Apple developers.

In this way, onAppear is called *when the mainview.overlay() is called * when overlay is unhidden *but not when overlay is not completely hidden but new appearance is called. onDisappear is called *when overlay is completely hidden (end of animation) *but not when overlay hiding animation is over but new overlay unhiding is called.

No comments:

Post a Comment