核心动画之CAAnimationGroup动画组和CATransition 转场动画以及UIView的转场动画

蓬野
2023-12-01

1.动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup 对象加入层后,组中所有动画对象可以同时并发运行

1.1属性说明:

animations:用来保存一组动画对象的NSArray ·默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间。

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

//    [self rotateAnimation];

//    [self moveAnimation];

    //动画组

    CAAnimationGroup *group = [CAAnimationGroup animation];

    CAAnimation *animation1 = [self moveAnimation];

    CAAnimation *animation2 = [self rotateAnimation];

    group.animations = @[animation1,animation2];

    

    //动画组的持续时间

    group.duration = 5;

    group.repeatCount = MAXFLOAT;

    [self.myImageView.layer addAnimation:group forKey:nil];

}

 

-(CAAnimation *)moveAnimation{

 

    //1 animation对象

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    //path

    CGMutablePathRef path = CGPathCreateMutable();

    //画圆弧

    CGPathAddArc(path, NULL, self.view.center.x, self.view.center.y, 100, 0, 2*M_PI, 0);

    animation.path = path;

    animation.duration = 5;

    animation.repeatCount = MAXFLOAT;//无限大

//    [self.myImageView.layer addAnimation: animation forKey:nil];

   return animation;

}

 

-(CAAnimation*)rotateAnimation{

    

    //1 animation对象

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

    

    //2

    animation.duration = 0.2;

    animation.repeatCount = MAXFLOAT;

    

    CGFloat angle = M_PI/30;

    animation.values = @[@0,@(-angle),@0,@(angle),@0];    

//    [self.myImageView.layer addAnimation:animation forKey:nil];

    return animation;

}

 

2.转场动画CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出 屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点

UINavigationController就是通过CATransition实现了将控制器的视图推入屏 幕的动画效果

2.1 动画属性:
type:动画过渡类型

subtype:动画过渡方向

startProgress:动画起点(在整体动画的百分比)

endProgress:动画终点(在整体动画的百分比) 

 

- (void)viewDidLoad {

  [super viewDidLoad];

   index = 0;

  images = [NSMutableArray arrayWithCapacity:6];

  

  for (int i =0; i<6; i++) {

    NSString *string = [NSString stringWithFormat:@"%d.jpg",i+1];

    UIImage *iamge = [UIImage imageNamed:string];

    [images addObject:iamge];

  }

  

  self.imageView.image = [UIImage imageNamed:@"1.jpg"];

  self.imageView.userInteractionEnabled = YES;//用户交互

  UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(changeImage)];

  swipe.direction = UISwipeGestureRecognizerDirectionLeft;

  

  [self.imageView addGestureRecognizer:swipe];

  

}

 

-(void)changeImage{

  index++;

  

  if (index>=6) {

    index =0;

    

  }

  

  self.imageView.image = [images objectAtIndex:index];

    

    //转场动画

    CATransition *trans = [[CATransition alloc]init];

    //属性

    //fade, `moveIn', `push' and `reveal

    trans.type = @"suckEffect";//动画类型

    trans.duration =2;

    //动画过渡方向

    //fromLeft', `fromRight', `fromTop' and  fromBottom

    trans.subtype =@"fromRight";

    

    [self.imageView.layer addAnimation:trans forKey:nil];

    

}

 3.UIView的转场动画

 + (void)transitionWithView:(UIView *)view duration:
    (NSTimeInterval)duration options:(UIViewAnimationOptions)options
    animations:(void (^)(void))animations completion:(void (^)(BOOL
    finished))completion;

参数说明:

duration:动画的持续时间

view:需要进行转场动画的视图

options:转场动画的类型

animations:将改变视图属性的代码放在这个block中

completion:动画结束后,会自动调用这个block 

 

#import "ViewController.h"

 

@interface ViewController ()

{

 

    UIView *view1;

    UIView *view2;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 200, 200)];

    view1.backgroundColor = [UIColor redColor];

    [self.view addSubview:view1];

    

    

    view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 200, 200)];

    view2.backgroundColor = [UIColor greenColor];

    [self.view addSubview:view2];

    

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

 

    //单转场动画

    /*

    [UIView transitionWithView:self.view duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

        //该视图的属性变化

        

        view2.alpha = 0;

        

        

    } completion:^(BOOL finished) {

        

        //完成后的调用

    }];

    */

    

    //双转场动画

    //注意当做完动画后,fromView会从父视图中移除

    NSLog(@"subview before: %@",self.view.subviews);

    [UIView transitionFromView:view2 toView:view1 duration:2 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {

 

        NSLog(@"subview after: %@",self.view.subviews);

        

    }];

    

}

 

 

 

转载于:https://www.cnblogs.com/xiaodiandian/p/5774243.html

 类似资料: