經過幾次嘗試,我終於得到了我的動畫工作。有幾件事要注意我們的。
如果你想轉換到錨定在中心,那麼你應該在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