1

我在屏幕上画一个圆圈,当用户点击一个按钮时。动画持续时间已经设置,并且还设置了从和到的值。在长按事件上画一个圆圈

我想要实现的动画应该以用户长按按钮的方式开始,直到他在屏幕上保持水龙头,即长按的持续时间。 只要用户抬起手指,圆圈就应该停止到它到目前为止完成的位置。

这里是我的代码:

-(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]; 
} 

这不是给我想要的效果之画圆,直到用户有他的手指在屏幕上。

请在代码中建议一些东西以获得所需的功能。

在此先感谢。

回答

2

你要遵循苹果的指导方针https://developer.apple.com/library/ios/qa/qa1673/_index.html

因此,在你的界面我会声明如下

@interface ViewController() 

@property (nonatomic, strong) CAShapeLayer *circle; 
@property (nonatomic, strong) CABasicAnimation *drawAnimation; 
@property (strong, nonatomic) IBOutlet UIButton *circleButton; 


@end 

然后在视图没有负载

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    int radius = 50; 
    self.circle = [CAShapeLayer layer]; 
    // Make a circular shape 
    self.circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) 
              cornerRadius:radius].CGPath; 
    // Center the shape in self.view 
    self.circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, 
           CGRectGetMidY(self.view.frame)-radius); 

    // Configure the apperence of the circle 
    self.circle.fillColor = [UIColor clearColor].CGColor; 
    self.circle.strokeColor = [UIColor redColor].CGColor; 
    self.circle.lineWidth = 5; 

    self.circle.strokeEnd = 0.0f; 

    // Add to parent layer 
    [self.view.layer addSublayer:_circle]; 

    // Target for touch down (hold down) 
    [self.circleButton addTarget:self action:@selector(startCircleAnimation) forControlEvents:UIControlEventTouchDown]; 

    // Target for release 
    [self.circleButton addTarget:self action:@selector(endCircleAnimation) forControlEvents:UIControlEventTouchUpInside]; 

    /** 
    Don't start Animation in viewDidLoad to achive the desired effect 
    */ 


} 

功能启动动画和恢复它(可能需要一个更好的名字)

-(void)startCircleAnimation{ 
    if (_drawAnimation) { 
     [self resumeLayer:_circle]; 
    } else { 
     [self circleAnimation]; 
    } 
} 

函数来结束动画

-(void)endCircleAnimation{ 
    [self pauseLayer:_circle]; 
} 

函数生成动画

- (void)circleAnimation 
{ 
    // Configure animation 
    self.drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    self.drawAnimation.duration   = 10.0; 
    self.drawAnimation.repeatCount   = 1.0; // Animate only once.. 


    // Animate from no part of the stroke being drawn to the entire stroke being drawn 
    self.drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 

    // Set your to value to one to complete animation 
    self.drawAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 

    // Experiment with timing to get the appearence to look the way you want 
    self.drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

    // Add the animation to the circle 
    [self.circle addAnimation:_drawAnimation forKey:@"draw"]; 
} 

暂停和苹果

- (void)pauseLayer:(CALayer*)layer 
{ 
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
    layer.speed = 0.0; 
    layer.timeOffset = pausedTime; 
} 

    - (void)resumeLayer:(CALayer*)layer 
    { 
     CFTimeInterval pausedTime = [layer timeOffset]; 
     layer.speed = 1.0; 
     layer.timeOffset = 0.0; 
     layer.beginTime = 0.0; 
     CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; 
     layer.beginTime = timeSincePause; 
    } 

停止功能的主要点,你会想从这个拿就是你没有记住按钮按下多长时间才能记住你正在记录的事件E从按钮UIControlEventTouchDown发送和UIControlEventTouchUpInside

编辑:的Gif

Animation


+0

非常感谢。有效!!!我在我的Long press handle方法中使用了开始和暂停方法,它的工作方式就像一个魅力。 – 2015-04-01 05:44:34

+0

不用担心:)我也想看一下拖延暂停动画可能会使它看起来更自然http://stackoverflow.com/questions/920675/how-can-i-delay-a-method-call-对于-1-第二 – 2015-04-01 05:51:40

0

我认为你应该让价值与计数器的价值相关,这将使绘图从上次用户抬起手指时留下的东西开始。

你也应该让你的计时器的时间间隔更小,1秒太长,0.1秒会更好。

+0

我已经作出相对于柜台我toValue。此外,我必须保持动画持续时间15秒,因此计时器为1秒。请建议什么应该是适当的和从停止动画圈到用户抬起他的手指点的价值。 fromValue的 – 2015-03-31 10:19:15

+0

应该是上次的值,并且使计时器的间隔更小使您的动画更加准确地响应。 – CarmeloS 2015-03-31 10:21:36

+0

我按照你的说法做了,但它只是使动画速度逐渐增加,没有别的。 – 2015-03-31 10:32:12