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

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 [email protected]"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);

}];

}

时间: 2024-12-23 08:24:09

核心动画之CAAnimationGroup动画组和CATransition 转场动画以及UIView的转场动画的相关文章

核心动画 (CAAnimationGroup)

Main.storyboard ViewController.m // //  ViewController.m //  8A05.核心动画 CAAnimationGroup // //  Created by huan on 16/2/5. //  Copyright © 2016年 huanxi. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (we

为UIView视图切换添加动画效果

我们定义了一个动画类来实现视图切换的动画效果,这个类只包含一个类方法,可直接调用,具体代码如下: 头文件: + ? 1 2 3 4 5 6 7 8 9 10 11 12 13 #import <Foundation/Foundation.h> @interface ViewAnimation : NSObject /*============================页面切换的方法==============================     View1 表示当前页面     Vi

影视动画 Premiere视频教程 AE CS5专业入门教程 绘声绘影X6入门提高 3DS MAX三维动画教程

热门推荐电脑办公计算机基础知识教程 Excel2010基础教程 Word2010基础教程 PPT2010基础教程 五笔打字视频教程 Excel函数应用教程 Excel VBA基础教程 WPS2013表格教程 更多>平面设计PhotoshopCS5教程 CorelDRAW X5视频教程 Photoshop商业修图教程 Illustrator CS6视频教程 更多>室内设计3Dsmax2012教程 效果图实例提高教程 室内设计实战教程 欧式效果图制作实例教程 AutoCAD2014室内设计 Aut

影视动画Premiere视频教程 AE CS5专业入门教程 绘声绘影X6入门提高 3DS MAX三维动画教程

热门推荐电脑办公计算机基础知识教程 Excel2010基础教程 Word2010基础教程 PPT2010基础教程 五笔打字视频教程 Excel函数应用教程 Excel VBA基础教程 WPS2013表格教程 更多>平面设计PhotoshopCS5教程 CorelDRAW X5视频教程 Photoshop商业修图教程 Illustrator CS6视频教程 更多>室内设计3Dsmax2012教程 效果图实例提高教程 室内设计实战教程 欧式效果图制作实例教程 AutoCAD2014室内设计 Aut

实现多个UIView之间切换的动画效果

@interface RootViewController (){ UIView *view1; UIView *view2; int flag; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; flag = 1; UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(130, 65, 50, 35)]; [

iOS动画开发之三——UIView的转场切换

iOS动画开发之三--UIView的转场切换 前两篇博客中,我们分别介绍了UIView动画的两种使用方式,分别为,带block的方式:http://my.oschina.net/u/2340880/blog/484457 ,传统的属性配置的方式:http://my.oschina.net/u/2340880/blog/484538.通过UIView动画的类方法,我们可以十分方便的使View某些属性改变的同时拥有动画效果.这篇博客主要讨论View切换的动画操作. 两个方法: + (void)tra

coreAnimation核心动画(四)CAAnimationGroup

1 // 2 // ViewController.m 3 // CAAnimationGroup 4 // 5 // Created by ys on 15/11/22. 6 // Copyright (c) 2015年 ys. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (weak, nonatomic) IBOutle

iOS控件——UIView与UIImageView播放动画的实现方法

1.UIView //初始状态 [UIView animateWithDuration:(int) animations:^{ //最终状态 }completion:^(BOOL finished){ //动画完成后需要做的事情 }]; 2.UIView //初始状态 [UIView startAnimation]; [UIView setAnimationDuration:(int)]; //最终状态 [UIView commitAnimation]; 3.UIImageView myImag

实现UIView的无限旋转动画(非CALayer动画)

效果: 素材: 源码: // // ViewController.m // Animation // // Created by YouXianMing on 15/2/5. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView