我在屏幕上画一个圆圈,当用户点击一个按钮时。动画持续时间已经设置,并且还设置了从和到的值。在长按事件上画一个圆圈
我想要实现的动画应该以用户长按按钮的方式开始,直到他在屏幕上保持水龙头,即长按的持续时间。 只要用户抬起手指,圆圈就应该停止到它到目前为止完成的位置。
这里是我的代码:
-(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