美文网首页
CoreAnimation 学习笔记4— CAAnimation

CoreAnimation 学习笔记4— CAAnimation

作者: DylanPP | 来源:发表于2018-06-05 10:28 被阅读15次

CAAnimationGroup

动画组 多动画的统一控制管理

上篇文章中简单的实现了平移,抖动,这里我们通过group将他们组合起来。

示例

    CABasicAnimation *animation = [self createbasicAnimationWithFromPoint:CGPointautoreversesMake(0,0) tovalue:CGPointMake(300, 0)    timingFunction:kCAMediaTimingFunctionEaseInEaseOut];
    CAKeyframeAnimation *animation1 = [self createShakeAni];
     CABasicAnimation * animation2 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    animation2.toValue = @50;
    animation2.duration = 2;
    animation2.fillMode = kCAFillModeForwards;
    animation2.removedOnCompletion = NO;
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.duration = 2;
    group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = NO;
    group.animations = @[animation,animation1,animation2];
    group.repeatCount = CGFLOAT_MAX;
    [self.redlayer addAnimation:group forKey:@"GroupAni"];
组合动画

CAAnimationGroup是CAAnimation动画并发执行的管理者,本身状态取决于其子动画的状态。

CATransition

转场动画

type 过度效果
type 参数 说明
fade kCATransitionFade 淡出效果
movein kCATransitionMoveIn 新视图移动到旧视图上
push kCATransitionPush 新视图推出旧视图
reveal kCATransitionReveal 移开旧视图显示新视图

字符串访问

字符串 说明
cube 立方体翻转效果
oglFlip 翻转效果
suckEffect 收缩效果
rippleEffect 水滴波纹效果
pageCurl 向上翻页效果
pageUnCurl 向下翻页效果
cameralIrisHollowOpen 摄像头打开效果
cameraIrisHollowClose 摄像头关闭效果
subtype 过渡方向
方向参数 说明
kCATransitionFromRight 从右侧转场
kCATransitionFromLeft 从左侧转场
kCATransitionFromTop 从上侧转场
kCATransitionFromBottom 从下侧转场

示例

    self.backgroudImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo"]];
    self.backgroudImageView.frame = CGRectMake(0, 0, 150, 200);
    self.backgroudImageView.center = self.view.center;
    self.backgroudImageView.backgroundColor = [UIColor grayColor];
    self.backgroudImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.backgroudImageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    tap.numberOfTapsRequired = 1;
    [tap addTarget:self action:@selector(createTransitionAniamtion:)];
    [self.backgroudImageView addGestureRecognizer:tap];
    
- (void)createTransitionAniamtion:(UITapGestureRecognizer *)tap {
    CATransition *animation = [CATransition animation];
    animation.duration = 5;
    animation.fillMode = kCAFillModeForwards;
    animation.type = @"rippleEffect";
    animation.subtype = kCATransitionFromBottom;
    [self.backgroudImageView.layer addAnimation:animation forKey:@"ripple"];
    self.backgroudImageView.image = [UIImage imageNamed:@"img05"];
}
示例转场

CAAnimation 简单使用

咻一咻

示例

- (void)createXiuAnimation {

    CAShapeLayer *aniLayer = [CAShapeLayer layer];
    aniLayer.frame = self.view.layer.bounds;
    UIBezierPath *roundPath = [UIBezierPath bezierPath];
    [roundPath addArcWithCenter:self.view.center radius:150 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    aniLayer.path = roundPath.CGPath;
    aniLayer.fillColor = [UIColor greenColor].CGColor;
    aniLayer.opacity = 0.0;
    //复制4份aniLayer
    _copyLayer = [CAReplicatorLayer layer];
    _copyLayer.frame = aniLayer.bounds;
    _copyLayer.instanceCount = 4;
    _copyLayer.instanceDelay = 1;
    [_copyLayer addSublayer:aniLayer];
    [self.view.layer addSublayer:_copyLayer];
    
    CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAni.fromValue = @(0.4);
    opacityAni.toValue = @(0.0);
    
    CABasicAnimation *scaleAni = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAni.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)];
    scaleAni.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0)];
    
    CAAnimationGroup *groupAnima = [CAAnimationGroup animation];
    groupAnima.animations = @[opacityAni, scaleAni];
    groupAnima.duration = 4.0;
    groupAnima.autoreverses = NO;
    groupAnima.repeatCount = CGFLOAT_MAX;
    [aniLayer addAnimation:groupAnima forKey:@"groupAnimation"];
}
- (void)controlAni:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        CFTimeInterval pausedTime = [_copyLayer convertTime:CACurrentMediaTime() fromLayer:nil];
        _copyLayer.speed = 0.0;
        _copyLayer.timeOffset = pausedTime;
    }else {
        CFTimeInterval pausedTime = [_copyLayer timeOffset];
        _copyLayer.speed = 1.0;
        _copyLayer.timeOffset = 0.0;
        _copyLayer.beginTime = 0.0;
        CFTimeInterval timeSincePause = [_copyLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
        _copyLayer.beginTime = timeSincePause;
    }
}

GithubZhaoBinLe

相关文章

网友评论

      本文标题:CoreAnimation 学习笔记4— CAAnimation

      本文链接:https://www.haomeiwen.com/subject/azhpsftx.html