【发布时间】:2013-02-12 14:57:53
【问题描述】:
当按下并按住按钮时,我有一个 UIImageView 在屏幕上运行。按下按钮时会更改 UIImageView 的 UIImage,而当松开按钮时,我会将其更改为原始 UIImage。当图像变回时,它会捕捉回图像开始的位置。
当按钮被按下时这个定时器被调用:
//This is the image that changes when the button is pressed.
imView.image = image2;
runTimer = [NSTimer scheduledTimerWithTimeInterval:0.04
target:self
selector:@selector(perform)
userInfo:nil
repeats:YES];
当按钮停止被按住时调用:
- (IBAction)stopPerform:(id)sender{
[runTimer invalidate];
//THIS IS WHAT SNAPS THE ANIMATION BACK:
//Without this the animation does not snap back
imView.image = image1;
}
- (void)performRight{
CGPoint point0 = imView.layer.position;
CGPoint point1 = { point0.x + 4, point0.y };
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
anim.fromValue = @(point0.x);
anim.toValue = @(point1.x);
anim.duration = 0.2f;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// First we update the model layer's property.
imView.layer.position = point1;
// Now we attach the animation.
[imView.layer addAnimation:anim forKey:@"position.x"];
}
我需要将图像的变化添加到动画中吗?如果有怎么办?我真的很困惑。
【问题讨论】:
标签: ios cocoa-touch core-animation caanimation