经过几次尝试,我终于得到了我的动画工作。有几件事要注意我们的。
如果你想转换到锚定在中心,那么你应该在CALayer:
致电anchorPoint
之前添加以它的父!否则,子视图将从anchorPoint
位置开始切割。
CALayer *layer = _completionIndicator.layer;
layer.anchorPoint = CGPointMake(.5, .5);
裹在一个runAnimationGroup
块动画调用(_completionIndicator
是定义为NSView
一个属性,centerInSuperview
定义如下):
[self.contentView addSubview:_completionIndicator];
[_completionIndicator centerInSuperview];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
CGFloat duration = <#duration#>;
CALayer *layer = _completionIndicator.layer;
CABasicAnimation *animation;
//Transform
animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(<#scale#>, <#scale#>, 1.0)];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
[animation setDuration:duration];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:@"zoomIn"];
//Fade
animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:1.0];
[animation setDuration:duration];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:@"fadeIn"];
} completionHandler:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[NSThread sleepForTimeInterval:<#seconds#>];
dispatch_sync(dispatch_get_main_queue(), ^{
[self hideCompletionIndicator];
});
});
}];
设置fillMode
是一个必不可少流畅的动画。默认值为kCAFillModeRemoved
,在第一次运行后运行动画时会导致跳转。
在OSX 10.11上,不需要在NSView
上设置wantsLayer
。我不知道以前的版本。
要隐藏视图,只需相反执行,并且移除在完成块中的视图:
- (void) hideCompletionIndicator
{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
CGFloat duration = <#duration#>;
CALayer *layer = _completionIndicator.layer;
CABasicAnimation *animation;
//Transform
animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(<#scale#>, <#scale#>, 1.0)];
[animation setDuration:duration];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:@"zoomOut"];
//Fade
animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.0];
[animation setDuration:duration];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:@"fadeOut"];
} completionHandler:^{
[_completionIndicator removeFromSuperview];
}];
}
的NSView类别执行
@implementation NSView (Additions)
- (void) centerInSuperview
{
[self centerInView:[self superview]];
}
- (void) centerInView:(NSView *)otherView
{
int w = self.frame.size.width;
int h = self.frame.size.height;
int x = (otherView.frame.size.width - w)/2;
int y = (otherView.frame.size.height - h)/2;
self.frame = NSMakeRect(x, y, w, h);
}
@end
的NSView类别接口
@interface NSView (Additions)
- (void) centerInSuperview;
- (void) centerInView:(NSView *)otherView;
@end
“NSCollectionView”的超级视图是否也支持层? –
@Rob:通过superview,你的意思是包含'NSCollectionView'的视图吗?不,这不对。原因是因为它是一个透明窗口,我不希望视图变成丑陋的灰色。 – Catastrophe