ell*_*zan 5 cocoa core-animation objective-c
我有一个非常简单的问题,我把头发拉过来.我试图动画NSButton的移动.现在,我意识到这几乎过于简单:
[[button animator] setFrame:newCGRect];
Run Code Online (Sandbox Code Playgroud)
但是,我想增加按钮行进所需的时间.所以,我研究了稍微复杂的CABasicAnimation.我想我正在做的一切都正确,但NSButton根本就没有动.
CGPoint center = CGPointMake(10, 20);
CALayer *layer = button.layer;
layer.position = CGPointMake(button.frame.size.width / 2, button.frame.size.height / 2);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [layer valueForKey:@"position"];
[animation setToValue:[NSValue valueWithPoint:center]];
[animation setFillMode:kCAFillModeForwards];
[animation setRemovedOnCompletion:NO];
[animation setDuration:3.0];
[layer addAnimation:animation forKey:@"position"];
Run Code Online (Sandbox Code Playgroud)
这里,按钮是NSButton.我正在将图层的位置设置到按钮的中心,因为我认为 "位置"必须指向中心.然后,我正在做的就是移动(据说)按钮到中心,在一个3秒长的动画中.我将动画添加到图层,然后没有任何反应.
现在,有人知道我做错了什么吗?或者,在旁注中,无论如何都使用动画属性并设置动画的长度?
事实证明我原来的代码有点矫枉过正,下面 Jason Harwig 的答案更准确。唯一的问题是,当对 NSView 子类(在本例中是 NSButton)进行动画处理时,他没有按照给定的持续时间进行动画处理。为了解决这个问题,下面是在给定持续时间内动画的代码:
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:8];
[[button animator] setFrame:newRect];
[NSAnimationContext endGrouping];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1097 次 |
| 最近记录: |