2015-12-01 79 views
0

我正在嘗試製作一個脈動圓。但是在下調規模後,會出現閃爍的效果。任何想法,爲什麼這是?這是我的代碼到目前爲止:在簡單的動畫鏈中閃爍

- (instancetype)init 
{ 
    self = [super init]; 
    if (self) { 
     if (!self.path) { 
      self.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:7 startAngle:0.0*(M_PI/180.0) endAngle:360.0*(M_PI/180.0) clockwise:YES].CGPath; 
     } 

     [self animateCircleUpward]; 
    } 
    return self; 
} 

-(void)animateCircleUpward { 

    [CATransaction begin]; 
     [CATransaction setCompletionBlock:^{[self animateCircleDownward];}]; 

     CABasicAnimation * scaleUpwardAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"]; 
     scaleUpwardAnimation.fromValue = @(0.7); 
     scaleUpwardAnimation.toValue = @(1.0); 


     CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
     opacityAnimation.fromValue = @(1.0); 
     opacityAnimation.toValue = @(0.6); 
     self.fillColor = [UIColor greenColor].CGColor; 

     CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 
     NSArray *animations = @[scaleUpwardAnimation, opacityAnimation]; 
     animationGroup.duration = 0.4; 

     animationGroup.animations = animations; 
     [self addAnimation:animationGroup forKey:@"pulse"]; 

    [CATransaction commit]; 
} 

-(void)animateCircleDownward { 

    [CATransaction begin]; 
    [CATransaction setCompletionBlock:^{[self animateCircleUpward];}]; 

    CABasicAnimation * scaleDownwardAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"]; 
    scaleDownwardAnimation.fromValue = @(1.0); 
    scaleDownwardAnimation.toValue = @(0.7); 


    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    opacityAnimation.fromValue = @(0.6); 
    opacityAnimation.toValue = @(1.0); 
    self.fillColor = [UIColor greenColor].CGColor; 

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 
    NSArray *animations = @[scaleDownwardAnimation, opacityAnimation]; 
    animationGroup.duration = 0.4; 

    animationGroup.animations = animations; 
    [self addAnimation:animationGroup forKey:@"pulse"]; 

    [CATransaction commit]; 
} 

@end 

Thanx提前!

+0

你檢查了這個答案:http://stackoverflow.com/a/25611323/1953178 –

回答

1

關於您的實際問題,您可能需要將CAAnimationGroup的fillMode屬性設置爲kCAFillModeForward。默認值是kCAFillModeRemoved,所以您可能會在Down和Up方法之間來回調用非呈現層。

也就是說,您試圖創建的是通過CATransaction的completionBlock在Up和Down方法之間來回調用的循環動畫。這似乎是一種令人難以置信的低效方法;爲什麼不嘗試更多的東西一樣:

-(void)scaleAndOpacityAnimations 
{ 
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy」]; 
    scaleAnimation.fromValue = @(0.7f); 
    scaleAnimation.toValue = @(1.0f); 

    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@「opacity」]; 
    opacityAnimation.fromValue = @(1.0f); 
    opacityAnimation.toValue = @(0.6f); 

    CAAnimationGroup *scaleAndOpacity = [CAAnimationGroup animation]; 
    scaleAndOpacity.animations = @[scaleAnimation, opacityAnimation]; 
    scaleAndOpacity.autoReverses = YES; 
    scaleAndOpacity.repeatCount = HUGE_VALF; 
    //OP had no value indicated for the duration in posted code, I’m including it here for completion 
    scaleAndOpacity.duration = 0.5f; 

    [self addAnimation:scaleAndOpacity forKey:@「pulse」]; 
} 

這將簡單地重複無限次,而不必在每個週期實例化新CAAnimations。