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

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

案例源码在我的主页里。实现效果图
IOS开发基础之核心动画 基础动画、关键帧、组动画案例_第1张图片

//
//  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

你可能感兴趣的:(IOS,ios)