Thursday, April 14, 2016

UIView.animateWithDuration Not Animating Swift (again)

Leave a Comment

Note: I’ve already checked the following stack overflow issues:

27907570, 32229252, 26118141, 31604300

All I am trying to do is fade animate in a view (by alpha) when called by an IBAction attached to a button. Then reverse when a button on the view is hit.

My wrinkle may be that I'm using a secondary view that is on the ViewDock in the storyboard View. The view is added to the subview at the time of viewDidLoad where the frame/bounds are set to the same as the superview (for a full layover)

The reason this is done as an overlay view since it is a tutorial indicator.

The result (like many others who've listed this problem) is that the view (and contained controls) simply appears instantly and disappears as instantly. No fade.

I have tried animationWithDuration with delay, with and without completion, with transition, and even started with the old UIView.beginAnimations.

Nothing is working. Suggestions warmly welcomed.

The code is about as straight forward as I can make it:
Edit: Expanded the code to everything relevant
Edit2: TL;DR Everything works with the exception of UIViewAnimateWithDuration which seems to ignore the block and duration and just run the code inline as an immediate UI change. Solving this gets the bounty

@IBOutlet var infoDetailView: UIView! // Connected to the view in the SceneDock  override func viewDidLoad() {     super.viewDidLoad()      // Cut other vDL code that isn't relevant      setupInfoView() }  func setupInfoView() {     infoDetailView.alpha = 0.0     view.addSubview(infoDetailView)     updateInfoViewRect(infoDetailView.superview!.bounds.size) }  func updateInfoViewRect(size:CGSize) {     let viewRect = CGRect(origin: CGPointZero, size: size)      infoDetailView.frame = viewRect     infoDetailView.bounds = viewRect      infoDetailView.layoutIfNeeded()     infoDetailView.setNeedsDisplay() }  override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {     super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)     updateInfoViewRect(size) }  func hideInfoView() {     AFLog.enter(thisClass)     UIView.animateWithDuration(         2.0,         animations:         {             self.infoDetailView.alpha = 0.0         },         completion:         { (finished) in             return true         }     )     AFLog.exit(thisClass) }  func showInfoView() {     AFLog.enter(thisClass)     UIView.animateWithDuration(         2.0,         animations:         {             self.infoDetailView.alpha = 0.75         },         completion:         { (finished) in             return true         }     )     AFLog.exit(thisClass) }  // MARK: - IBActions  @IBAction func openInfoView(sender: UIButton) {     showInfoView() }  @IBAction func closeInfoView(sender: UIButton) {     hideInfoView() } 

Please note, I started with the following:

func showInfoView() {     UIView.animateWithDuration(2.0, animations: { () -> Void in         self.infoDetailView.alpha = 0.75     }) }  func hideInfoView() {     UIView.animateWithDuration(2.0, animations: { () -> Void in         self.infoDetailView.alpha = 0.00     }) } 

7 Answers

Answers 1

There are several strange things I can see,

first, remove:

infoDetailView.layoutIfNeeded() infoDetailView.setNeedsDisplay() 

Usually you don't need to call those methods manually unless you know exactly what you are doing.

Also, when you are changing the size:

infoDetailView.frame = viewRect infoDetailView.bounds = viewRect 

You never need to set both bounds and frame. Just set frame.

Also, you should probably make sure that the view actually doesn't ignore the frame by setting:

infoDetailView.translatesAutoresizingMaskIntoConstraints = true 

Instead of resetting the frame, just set autoresize mask:

infoDetailView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 

Resulting in:

override func viewDidLoad() {     super.viewDidLoad()      // Cut other vDL code that isn't relevant      setupInfoView() }  func setupInfoView() {     infoDetailView.alpha = 0.0     infoDetailView.translatesAutoresizingMaskIntoConstraints = true     infoDetailView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]     infoDetailView.frame = view.bounds     view.addSubview(infoDetailView) }  func hideInfoView() {   ... } 

I think this should actually help because immediate animations are often connected to size problems.

If the problem persists, you should check whether the infoDetailView in your animation is the same object as the infoDetailView you are adding to the controller.

Answers 2

Use this code:

UIView.animateWithDuration(0.3, animations: { () -> Void in     self.infoDetailView.alpha = 0.0  }) 

Answers 3

Make sure infoDetailView's opaque is false.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/opaque

This property provides a hint to the drawing system as to how it should treat the view. If set to true, the drawing system treats the view as fully opaque, which allows the drawing system to optimize some drawing operations and improve performance. If set to false, the drawing system composites the view normally with other content. The default value of this property is true.

Answers 4

Try Below code. Just play with alpha and duration time to perfect it.

Hide func

func hideInfoView() { AFLog.enter(thisClass) UIView.animateWithDuration(     2.0,     animations:     {         self.infoDetailView.alpha = 0.8     },     completion:     { (finished) in         UIView.animateWithDuration(     2.0,     animations:     {         self.infoDetailView.alpha = 0.4     },     completion:     { (finished) in         self.infoDetailView.alpha = 0.0     } )     } ) AFLog.exit(thisClass) } 

Show func

func showInfoView() { AFLog.enter(thisClass) UIView.animateWithDuration(     2.0,     animations:     {         self.infoDetailView.alpha = 0.3     },     completion:     { (finished) in         UIView.animateWithDuration(     2.0,     animations:     {         self.infoDetailView.alpha = 0.7     },     completion:     { (finished) in         self.infoDetailView.alpha = 1.0     } )     } ) AFLog.exit(thisClass) } 

Answers 5

If the animation does not seem to execute then consider examining the state of each of your views, before you enter the animation block. For example, if the alpha is already set to 0.4 then the animation that adjusts your view alpha, will complete almost instantly, with no apparent effect.

Consider using a keyframe animation instead. This is what a shake animation in objective c looks like.

+(CAKeyframeAnimation*)shakeAnimation {     CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];     animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-10.0, 0.0, 0.0)],                          [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(10.0, 0.0, 0.0)]];     animation.autoreverses = YES;     animation.repeatCount = 2;     animation.duration = 0.07;     return animation; } 

Here is a post that shows you how to adjust alpha with keyframes http://stackoverflow.com/a/18658081/1951992

Answers 6

I've replicated your code and it work well, it's all ok. Probably you must control constraints, IBOutlet and IBActions connections. Try to isolate this code into a new project if it's necessary.

Update: my code and my storyboard and project folder photo:

my storyboard and project folder photo

Every object (view and buttons) are with default settings.

enter image description here

I've commented all AFLog lines (probably it's only any more "verbose mode" to help you) , the rest of your code is ok and it do what do you aspected from it, if you press open button the view fade in, and when you tap close button the view fade out.

PS Not relevant but i'm using xCode 7.3 , a new swift 2.2 project.

Answers 7

If you infoDetailView is under auto layout constraints you need to call layoutIfNeeded on the parent view inside animateWithDuration:

func showInfoView() {     self.view.layoutIfNeeded() // call it also here to finish pending layout operations     UIView.animateWithDuration(2.0, animations: { () -> Void in         self.infoDetailView.alpha = 0.75         self.view.layoutIfNeeded()     }) } 

Theoretically this should not be needed if you just change the .alpha value, but maybe this could be the problem in this case.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment