Layer Animation

1.Position and size 修改位置和尺寸
• bounds:
• position:
• transform:

  1. Border
    • borderColor:
    • borderWidth:
    • cornerRadius:

3.Shadow
shadowOffset:
• shadowOpacity: 阴影透明度
• shadowPath: 修改阴影的形状
• shadowRadius:

4.Contents
• contents: raw TIFF or PNG data
• mask: 遮罩
• opacity


fillMode
kCAFillModeRemoved 默认值
kCAFillModeForwards 动画结束后停留在最后一帧
kCAFillModeBackwards 动画开始前就在第一帧开始预备
kCAFillModeBoth kCAFillModeForwards&&kCAFillModeBackwards

问题
1.何时使用 [UIView animateWithDuration:...] ,[CABasicAnimation animationWithKeyPath...]?
答:如果有多个控件同时使用同一个动画效果,如果使用 animateWithDuration,每个控件都要写一次;而CABasicAnimation只需创建一次,添加每个控件的layer即可,时间延时使用CACurrentMediaTime()获取.比如

CABasicAnimation * flyRight = [CABasicAnimation animationWithKeyPath:@"position.x"];
    flyRight.fromValue = @(- self.view.frame.size.width/2);
    flyRight.toValue = @(self.view.frame.size.width / 2);
    flyRight.duration = 0.5;
    flyRight.fillMode = kCAFillModeBoth;
    [_header.layer addAnimation:flyRight forKey:nil];
    
    flyRight.beginTime = CACurrentMediaTime() + 0.3;
    [_userName.layer addAnimation:flyRight forKey:nil];
    _userName.layer.position = CGPointMake(self.view.frame.size.width * 0.5, _userName.layer.position.y);

keyPath表


Layer Animation_第1张图片
layerAnimation.gif

云朵的移动

-(void)animateCloud:(UIImageView *)imageView speed:(CGFloat )speed{
    speed = speed / self.view.frame.size.width;
    CGFloat duration  = (self.view.frame.size.width - imageView.frame.origin.x) * speed;
    
    [UIView animateWithDuration:(NSTimeInterval)duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect frame = imageView.frame;
        imageView.frame = CGRectMake(self.view.frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
    } completion:^(BOOL finished) {
        CGRect initFrame = imageView.frame;
        initFrame.origin.x = - imageView.frame.size.width;
        imageView.frame = initFrame;
        [self animateCloud:imageView];
    }];
}

按钮颜色变化

-(void)tintBackgroundColor:(CALayer *)layer toColor:(UIColor *)color{
    CABasicAnimation * tint = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    tint.fromValue = (__bridge id _Nullable)(layer.backgroundColor);
    tint.toValue = (__bridge id _Nullable)(color.CGColor);
    tint.duration = 1.0;
    [layer addAnimation:tint forKey:nil];
    layer.backgroundColor = color.CGColor;
}

圆角改变

-(void)roundCorners:(CALayer *)layer toCornerRadius:(CGFloat)cornerRadius{
    CABasicAnimation *round = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    round.fromValue = @(layer.cornerRadius);
    round.toValue = @(cornerRadius);
    round.duration =1.0;
    [layer addAnimation:round forKey:nil];
    layer.cornerRadius = cornerRadius;
}

tipMsg 用的是 transitionWithView

-(void)showMessage:(NSInteger)index{
     _label.text = _messages[index];
    [UIView transitionWithView:_status duration:0.33 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        self.status.hidden = NO;
    } completion:^(BOOL finished) {
        [self delay:2 completion:^{
            if (index < self.messages.count -1) {
                [self removeMessages:index];
            }
            else{
                [self resetForm];
            }
        }];
    }];
}

按钮恢复

-(void)resetForm{
    [UIView transitionWithView:_status duration:0.2 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
        //隐藏tipMsg
        self.status.hidden = YES;
        self.status.center = statusPosition;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 delay:0 options:0 animations:^{
            self.spinner.center = CGPointMake(-20, 16);
            self.spinner.alpha = 0;
            CGRect bounds = self.loginBtn.bounds;
            bounds.size.width -= 80;
            self.loginBtn.bounds = bounds;
            CGPoint center = self.loginBtn.center;
            center.y -=60;
            self.loginBtn.center = center;
            
        } completion:^(BOOL finished) {
            UIColor *tincolor = [UIColor colorWithRed:0.63 green:0.84 blue:0.35 alpha:1];
            [self tintBackgroundColor:self.loginBtn.layer toColor:tincolor];
            [self roundCorners:self.loginBtn.layer toCornerRadius:10.0];
             self.loginBtn.userInteractionEnabled = YES;
        }];
    }];
}

----from iOS_Animations_by_Tutorials_v2

你可能感兴趣的:(Layer Animation)