开发者

restarting CABasicAnimation

Im looping a gradient animation. The screen glows red when the user touches it. I have it looping using the animation delegate method to remove the layer and then call the animation at the end of every cycle. This works ok.

What I want to achieve is that if the user touches the screen again the animation is removed and restarts. it currently goes into a rapid feedback loop.

#import "AGradient.h"
#import <QuartzCore/CoreAnimation.h>


@implementation AGradient
@synthesize loopStat,animDuration,resetLoop;

-(void)animateGradient
{
    layer = [CAGradientLayer layer];
    layer.frame = self.bounds;

    color = [UIColor blackColor].CGColor;
    UIColor *color1 = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
    UIColor *color2 = [UIColor colorWithRed:0.80 green:0.0 blue:0.00 alpha:1.0];
    UIColor *color3 = [UIColor colorWithRed:0.40 green:0.0 blue:0.0 alpha:1.0];
    NSArray *colors = [NSArray arrayWithObjects:(id)[color1 CGColor],[color2 CGColor],[color3 CGColor],nil];

    layer.colors = [NSArray arrayWithObjects:(id)color,color,color, nil];
    layer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.4],[NSNumber numberWithFloat:0.9], nil];
    layer.startPoint = CGPointMake(0.5, 0);
    layer.endPoint = CGPointMake(0, 1);

    CABasicAnimation *animateLayer = [CABasicAnimation animationWithKeyPath:@"colors"]; 
    animateLayer.delegate = self;
    animateLayer.fromValue = colors;
    animateLayer.toValue = [NSArray a开发者_开发知识库rrayWithObjects:(id)color,color,color, nil];
    animateLayer.duration   = animDuration;
    animateLayer.removedOnCompletion = YES;
    animateLayer.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [layer addAnimation:animateLayer forKey:@"animation"];
    [self.layer insertSublayer:layer atIndex:0];
}




- (void)animationDidStart:(CAAnimation *)theAnimation {  
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{ 
    if (loopStat) {
    if (self.layer == nil) {
          NSLog(@"do nothing");

    } else
    {
        [layer removeAllAnimations];
        [layer removeFromSuperlayer];
        layer = nil;
       NSLog(@"remove layers");
    }
    NSLog(@"animate layers");
    [self animateGradient]; 
}

And in viewcontroller

- (void)viewDidLoad
{
    [super viewDidLoad];

    tmpGrad = [[[AGradient alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)]retain];
    [self.view addSubview:tmpGrad];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScreen:)];
    [singleTap setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];
}


- (void)tapScreen:(UIGestureRecognizer *)gestureRecognizer {
    tmpGrad.loopStat = 1;
    tmpGrad.animDuration = 2;
    [tmpGrad animateGradient];
}


I am not sure but when user touches screen again following lines should not execute if you want to remove animations.

tmpGrad.loopStat = 1;

tmpGrad.animDuration = 2;

[tmpGrad animateGradient];

Instead run some code that removes layer and then release it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜