IOS开发基础之核心动画 基础动画、关键帧、组动画案例

这篇博客介绍了iOS开发中的核心动画应用,包括基础动画、关键帧动画和组动画的实战案例。作者提供了案例源码,并展示了实现效果。
摘要由CSDN通过智能技术生成

IOS开发基础之核心动画 基础动画、关键帧、组动画案例

案例源码在我的主页里。实现效果图
在这里插入图片描述

//
//  ViewController.m
//  30-核心动画
//
//  Created by 鲁军 on 2021/2/21.
//

#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,weak)CALayer *layer;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
     UIView *redView =[[UIView alloc] init];
    redView.frame =CGRectMake(100, 100, 20, 20);
    redView.backgroundColor =[UIColor redColor];
    self.layer=redView.layer ;
    [self.view addSubview:redView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   //组动画
    CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
    //创建动画对象 (做什么动画)
    CABasicAnimation *anim = [[CABasicAnimation alloc]init];
    anim.keyPath = @"transform.rotation";
    anim.byValue =@(2*M_PI*10);
    CAKeyframeAnimation *anim1=[[CAKeyframeAnimation alloc] init];
    anim1.keyPath = @"position";
    UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:1];
    anim1.path = path.CGPath;
    group.animations=@[anim,anim1];
    group.duration = 3;
    group.repeatCount = INT_MAX; // 重复的次数
    //怎么做动画
    //添加动画(对谁做动画)
    [self.layer addAnimation:group forKey:nil];
}
//关键帧
-(void)test2{
    //关键帧
      //创建动画对象 (做什么动画)
      CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
      //怎么做动画
      anim.keyPath=@"position";
    /*  NSValue *v1 =[NSValue valueWithCGPoint:CGPointMake(100, 100)];
      NSValue *v2 =[NSValue valueWithCGPoint:CGPointMake(150, 100)];
      NSValue *v3 =[NSValue valueWithCGPoint:CGPointMake(100, 150)];
      NSValue *v4 =[NSValue valueWithCGPoint:CGPointMake(150,150 )];
      anim.values =@[v1,v2,v3,v4];
     */
      UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:1];
      anim.path = path.CGPath;
      anim.duration = 2;
      anim.repeatCount = INT_MAX; // 重复的次数
      //添加动画(对谁做动画)
      [self.layer addAnimation:anim forKey:nil];
}
//基础动画
-(void)testbaseAnimation{
    //创建动画对象 (做什么动画)
      CABasicAnimation *animation = [[CABasicAnimation alloc] init];
      //怎么做动画
      animation.keyPath = @"position.x";
      /*animation.fromValue =@(10); 从哪
      animation.toValue = @(300);  到哪
      */
      //每次加10px
      animation.byValue = @(10);
      //不回去原来的地方
      animation.fillMode = kCAFillModeForwards;
      animation.removedOnCompletion=NO;
      //添加动画(对谁做动画)
      [self.layer addAnimation:animation forKey:nil];
}
@end

//
//  LJView.m
//  30-核心动画
//
//  Created by 鲁军 on 2021/2/21.
//

#import "LJView.h"

@implementation LJView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:1];
    [path stroke];
    
}


@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值