简体   繁体   English

如何在Cocoa / Mac应用程序开发中使用UIView样式动画

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

I'm making the transition from programming iPhone to native Mac applications. 我正在从iPhone编程转换为原生Mac应用程序。 One part that I miss is the simplicity of the UIView animation system. 我想念的一部分是UIView动画系统的简洁性。

I had the following two methods for a UIView subclass: 我有一个UIView子类的以下两种方法:

-(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. 现在我不知道如何在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. 我有时会多次调用此函数(隐藏),而淡入淡出函数可能仍在运行。

This should produce the same result as your iOS code: 这应该产生与iOS代码相同的结果:

[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. 默认持续时间为.25秒。 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: 在快速4中更新rob mayoff的答案:

NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = 0.5
NSAnimationContext.current.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
myView.animator().alphaValue = 0
NSAnimationContext.endGrouping()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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