我在iOS中有一个非常简单的动画,它会淡化视图,调整容器大小以适应另一个视图,然后淡化其他视图。这很容易做到且非常简单。
我一直在尝试在OSX上做一些非常类似的事情,但我一直无法弄清楚如何做到这一点。与iOS相比,OSX上的动画效果非常笨重和难以使用。
任何帮助都将非常感谢!!
谢谢! :)
// Fade out viewOne, resize frame to fit viewTwo, fade in viewTwo
[UIView animateWithDuration: 0.15
animations: ^{
[viewOne setAlpha:0.0];
}
completion: ^(BOOL finished) {
[UIView animateWithDuration: 0.2
animations: ^{
[self setFrame: [viewTwo frame]];
}
completion: ^(BOOL finished) {
[viewTwo setAlpha: 0.0];
[self addSubview: viewTwo];
[UIView animateWithDuration: 0.15
animations: ^{
[viewTwo setAlpha:1.0];
}];
}];
}];
答案 0 :(得分:22)
在OSX上使用动画代理时,我编写了一个使用块来实现与上面基本相同的小类。
请注意,此类不是线程安全的,并且未经历任何特定或压力测试。
//Interface
@interface MZAnimator : NSObject{}
+ (void)animateWithDuration:(NSTimeInterval)duration
animation:(void (^)(void))animationBlock;
+ (void)animateWithDuration:(NSTimeInterval)duration
animation:(void (^)(void))animationBlock
completion:(void (^)(void))completionBlock;
@end
//Implementation
@interface MZAnimator ()
+ (void)runEndBlock:(void (^)(void))completionBlock;
@end
@implementation MZAnimator
+ (void)animateWithDuration:(NSTimeInterval)duration
animation:(void (^)(void))animationBlock
{
[self animateWithDuration:duration animation:animationBlock completion:nil];
}
+ (void)animateWithDuration:(NSTimeInterval)duration
animation:(void (^)(void))animationBlock
completion:(void (^)(void))completionBlock
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:duration];
animationBlock();
[NSAnimationContext endGrouping];
if(completionBlock)
{
id completionBlockCopy = [[completionBlock copy] autorelease];
[self performSelector:@selector(runEndBlock:) withObject:completionBlockCopy afterDelay:duration];
}
}
+ (void)runEndBlock:(void (^)(void))completionBlock
{
completionBlock();
}
@end
答案 1 :(得分:18)
您可以使用:
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[context setDuration:2.0];
//Animation code
} completionHandler:^{
//Completion Code
NSLog(@"Completed");
}];