當按下按鈕時,我有一個UIImageView在屏幕上運行。當按下按鈕時,將更改UIImageView的UIImage,並且當按鈕鬆開後,將其更改爲其原始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"];
}
我需要的圖像添加到動畫中的變化?如果是這樣如何?我真的很困惑。
NSTimer是我知道如何進行按住的唯一方法。你會怎麼做? – Tanner 2013-02-12 22:10:05
@Tanner我認爲計時器正在用於與動畫同步;如果不是,那就不用擔心。 – trojanfoe 2013-02-12 22:34:59
我有另一個問題抱歉,我將如何使用animationDidStop ...來更改imageView上的UIImage?它需要一個CAAnimation,但我只能使用aCABasicAnimation或完成塊更改圖像。謝謝 – Tanner 2013-02-12 22:45:16