简体   繁体   中英

Completely unhide view prior to UIView.animateWithDuration

I am using UIView.animateWithDuration to slide in a view upon tap recognition. This is working fine except, for the very first time, the view is unhidden slightly late and it suddenly appears mid-way through the animation. Subsequent show/hide operations do not have this issue.

I am implementing this as follows

    textBlurBackground.hidden = false
    descriptionOutlet.hidden = false
    UIView.animateWithDuration(0.4, delay: 0.0, options: nil, animations: {
        self.textBlurBackground.transform = CGAffineTransformMakeTranslation(0, 0)
        self.descriptionOutlet.transform = CGAffineTransformMakeTranslation(0, 0)
        }, completion: nil)

so it seems like, even though I'm setting the "hidden" property first (on the first two lines) this is somehow delayed and only executed while the animation is already running. Note that this only happens the first time when the view is created; subsequent show/hide operations work fine (the views are completely unhidden prior to the animation).

I notice there is a "completion" hook - is there something equivalent for a preparation phase? Ie don't start the animation until view is fully unhidden.

Thanks to the timing advice (and running things slowly) in the comments I could figure out that, in this case, it was not a timing issue but, rather, the dimensions of the views were somehow incorrect when the view was first loaded. Eg I have in viewDidLoad

override func viewDidLoad() {
    textBlurBackground.hidden = true
    descriptionOutlet.hidden = true
    descriptionOutlet.transform = CGAffineTransformMakeTranslation(-textBlurBackground.bounds.width, 0)
    textBlurBackground.transform = CGAffineTransformMakeTranslation(-textBlurBackground.bounds.width, 0)
}

the textBlurBackground.bounds.width is incorrect at that point; that code does not fully move those views off the screen by the negative width of the textBlueBackground view. But, once the view is loaded, subsequent show/hide operations do work correctly as it can then get the right size for the view.

Thus it seems this is an issue with auto-layout sizing where "textBlurBackground.bounds.width" is not correct upon initial view loading. Which is odd because it does not change size so I may need to specify some more constraints.

A view may take a few milliseconds to appear for the first time because it has to be rendered/drawn on screen, mostly if this view contains a lot of subviews. Are you instantiating that view in that same handleTap function? You should be instantiating the view either in the viewDidLoad method of your UIViewController, or in the init of your UIView. Set hidden what needs to be hidden right after instantiating the view. Next, in the handleTap , unhide them and do the animation like posted above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM