动画踩坑

1.对stroke做动画

stroke的意思是描边,对stroke做动画可以让我们对CAShapeLayer描绘的路径做动画效果,可用于进度条绘制等。
以下是一个进度条例子

    // 画背景灰色进度条
    CGRect progressRect = CGRectMake(100, 200, 200, 10);
    CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
    backgroundLayer.frame = progressRect;
    backgroundLayer.lineWidth = 10;
    backgroundLayer.lineCap = kCALineCapRound;
    backgroundLayer.strokeColor = [UIColor grayColor].CGColor;
    backgroundLayer.fillColor = [UIColor clearColor].CGColor;
    UIBezierPath *backPath = [UIBezierPath bezierPath];
    [backPath moveToPoint:CGPointMake(0, 5)];
    [backPath addLineToPoint:CGPointMake(200, 5)];
    backgroundLayer.path = backPath.CGPath;
    [self.view.layer addSublayer:backgroundLayer];
    // 画前景黄色进度条
    CAShapeLayer *foregroundLayer = [CAShapeLayer layer];
    foregroundLayer.frame = progressRect;
    foregroundLayer.lineWidth = 8;
    foregroundLayer.lineCap = kCALineCapRound;
    foregroundLayer.strokeColor = [UIColor yellowColor].CGColor;
    foregroundLayer.fillColor = [UIColor clearColor].CGColor;
    foregroundLayer.strokeEnd = 0.0f;
    UIBezierPath *forePath = [UIBezierPath bezierPath];
    [forePath moveToPoint:CGPointMake(.5f, 5.f)];
    [forePath addLineToPoint:CGPointMake(199.5f, 5.f)];
    foregroundLayer.path = forePath.CGPath;
    [self.view.layer addSublayer:foregroundLayer];
    // 做动画
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        CGFloat percent = arc4random() % 100 / 100.f;
        POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
        spring.toValue = @(percent);
        spring.springBounciness = 12.f;
        [foregroundLayer pop_addAnimation:spring forKey:nil];
    }];

2.利用CAGradientLayer做动画

利用CAGradientLayer做动画,一般都是通过设置某个View的layer的mask来实现的。mask的作用是:根据设置的layer某个点的alpha值来决定该layer这个点的alpha值。
CAGradientLayer的startPoint、endPoint、locations和colors都是可以做动画的。
startPoint和endPoint决定图层颜色渐变的方向,默认是从上往下。
colors决定要渐变的颜色,locations则决定渐变的分界点。这两个数组的count必须是相同的。

- (void)fadeAnimation {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"material"]];
    [self.view addSubview:imageView];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, -imageView.height / 2, imageView.width, imageView.height * 2);
    gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
    gradientLayer.locations = @[ @(0.0), @(0.25)];
    [self.view.layer addSublayer:gradientLayer];
    imageView.layer.mask = gradientLayer;
    NSLog(@"%f", imageView.width);
    CABasicAnimation *anim = [[CABasicAnimation alloc] init];
    anim.keyPath = @"locations";
    anim.duration = 2;
    anim.fromValue = @[ @(0.0), @(0.25)];
    anim.toValue = @[ @(0.75), @(1.0)];
    anim.repeatCount = 10;
    [gradientLayer addAnimation:anim forKey:nil];
}
动画踩坑_第1张图片
fadeAnim.png

3.正确的移除动画

removeAllAnimation不能随便调用,当你在remove某个动画之后想要添加其他动画,你会发现无论如何都添加不上去,正确的移除方式是根据你添加动画时候的key来移除对应的动画。

  [self.goldOreCatcher.layer removeAnimationForKey:kMiningMachineRotationAnimationKey];
  CABasicAnimation *anim = [[CABasicAnimation alloc] init];
   anim.keyPath = @"bounds";
    anim.duration = 5;// TODO 根据速度计算
    CGFloat targetLength = sqrtf(pow(lengthX, 2) + pow(lengthY, 2));
    anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, self.goldOreCatcher.width, targetLength)];
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    [self.goldOreCatcher.layer addAnimation:anim forKey:nil];

你可能感兴趣的:(动画踩坑)