I'm making the transition from programming iPhone to native Mac applications. One part that I miss is the simplicity of the UIView animation system.
I had the following two methods for a UIView subclass:
-(void) hide{
_isHidden=YES;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
self.alpha = 0;
[UIView commitAnimations];
}
-(void) show{
_isHidden=NO;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
self.alpha = 1;
[UIView commitAnimations];
}
Now I'm not sure how to accomplish this in Cocoa. I tried the following but I'm not sure it works as it should.
-(void) hide{
[[_myView animator] setAlpha:0];
}
I call this function (hide) multiple times sometimes while the fade function might still be running.
The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .
Layer animations work much like view animations; you simply animate a property between a start and an end value over a defined period of time and let Core Animation take care of the rendering in between.
To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.
Animations can add visual cues that notify users about what's going on in the app. In iOS, animations are used extensively to reposition views, change their size, remove them from view hierarchies, and hide them.
This should produce the same result as your iOS code:
[NSAnimationContext beginGrouping]; {
[[NSAnimationContext currentContext] setDuration:.5];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[_myView.animator setAlphaValue:0.0];
} [NSAnimationContext endGrouping];
The default duration is .25 seconds. I'm not sure what the default timing function is. If you're ok with the defaults, you can just say this:
[_myView.animator setAlphaValue:0.0];
Update of rob mayoff answer in swift 4:
NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = 0.5
NSAnimationContext.current.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
myView.animator().alphaValue = 0
NSAnimationContext.endGrouping()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With