重新启动 CABasicAnimation

Posted

技术标签:

【中文标题】重新启动 CABasicAnimation【英文标题】:restarting CABasicAnimation 【发布时间】:2011-09-05 17:04:12 【问题描述】:

我正在循环渐变动画。当用户触摸它时,屏幕会发出红光。我使用动画委托方法循环删除图层,然后在每个循环结束时调用动画。这工作正常。

我想要实现的是,如果用户再次触摸屏幕,动画将被移除并重新启动。它目前进入快速反馈循环。

#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 arrayWithObjects:(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]; 

在视图控制器中

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

【问题讨论】:

【参考方案1】:

我不确定,但是当用户再次触摸屏幕时,如果要删除动画,则不应执行以下行。

tmpGrad.loopStat = 1;

tmpGrad.animDuration = 2;

[tmpGrad animateGradient];

改为运行一些删除层然后释放它的代码。

【讨论】:

以上是关于重新启动 CABasicAnimation的主要内容,如果未能解决你的问题,请参考以下文章

收集应用程序内存后重新启动时启动画面未重新启动

描述用命令如何启动,停止,重新启动和查看www服务?

如果我们重新启动 postgres,如何重新启动后台工作人员 postgresql?

重新启动应用程序而不重新启动服务器?

重新启动节点时如何使节点检查器重新启动?

SQL2005重新启动时(提示无法停止MSSQLSERVER),该怎么办?