我在屏幕上畫一個圓圈,當用戶點擊一個按鈕時。動畫持續時間已經設置,並且還設置了從和到的值。在長按事件上畫一個圓圈
我想要實現的動畫應該以用戶長按按鈕的方式開始,直到他在屏幕上保持水龍頭,即長按的持續時間。 只要用戶擡起手指,圓圈就應該停止到它到目前爲止完成的位置。
這裏是我的代碼:
-(void)startCircularAnimation{
int radius = 50;
CAShapeLayer *circle = [CAShapeLayer layer];
// Make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure the apperence of the circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor redColor].CGColor;
circle.lineWidth = 5;
// Add to parent layer
[self.view.layer addSublayer:circle];
// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 15.0;
drawAnimation.repeatCount = 1.0; // Animate only once..
// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:counter/drawAnimation.duration];
// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"draw"];
}
此方法執行動畫和從值從我開始接觸開始長按處理方法的情況下,一個計時器計算。我無法獲得長時間完美的持續時間。
長按事件方法是這樣的。
- (void)_handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer{
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
{
counter = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(incrementCounter) userInfo:nil repeats:YES];
}
case UIGestureRecognizerStateEnded:{
NSLog(@"State ended");
[timer invalidate];
break;
}
case UIGestureRecognizerStateCancelled:{
NSLog(@"State cancelled");
break;
}
case UIGestureRecognizerStateFailed:
{
break;
}
default:
break;
}
}
和增量計數器方法如下
- (void)incrementCounter {
counter++;
[self startCircularAnimation];
}
這不是給我想要的效果之畫圓,直到用戶有他的手指在屏幕上。
請在代碼中建議一些東西以獲得所需的功能。
在此先感謝。
非常感謝。有效!!!我在我的Long press handle方法中使用了開始和暫停方法,它的工作方式就像一個魅力。 – 2015-04-01 05:44:34
不用擔心:)我也想看一下拖延暫停動畫可能會使它看起來更自然http://stackoverflow.com/questions/920675/how-can-i-delay-a-method-call-對於-1-第二 – 2015-04-01 05:51:40