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