Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do UIView style animations in Cocoa/Mac app development

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.

like image 577
user491880 Avatar asked Oct 28 '11 21:10

user491880


People also ask

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .

How layer animation is used in iOS?

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.

How do you animate a view in Swift?

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.

What is animated in Swift?

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.


2 Answers

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];
like image 83
rob mayoff Avatar answered Jan 04 '23 10:01

rob mayoff


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()
like image 27
kstefanou Avatar answered Jan 04 '23 09:01

kstefanou