iOS Core Animation (本人写的源码)
2015-12-28 10:39
337 查看
//
// ViewController.h
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController :
UIViewController
@end
// ViewController.m
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
NSString * const string =
@"123";
@interface
ViewController ()
@property (strong,
nonatomic) IBOutlet
UIImageView *imageView;
@property (strong,
nonatomic) IBOutlet
UIImageView *colorImageView;
@property(nonatomic,retain)
SecondViewController *secondVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.imageView.image=[UIImage
imageNamed:@"10042637.jpg"];
self.imageView.layer.cornerRadius=20;
self.imageView.layer.borderWidth=2;
self.imageView.layer.borderColor=[UIColor
greenColor].CGColor;
self.imageView.layer.shadowColor=[[UIColor
blueColor]
CGColor];
self.imageView.layer.shadowOffset=CGSizeMake(20,
20);
self.imageView.layer.shadowOpacity=0.5;
[self.imageView
setClipsToBounds:YES];
//超过主层边框范围的内容都裁剪
self.imageView.layer.masksToBounds=NO;
//[self testImageView];
self.colorImageView.backgroundColor=[UIColor
purpleColor];
self.colorImageView.layer.cornerRadius=20;
self.colorImageView.layer.borderWidth=2;
self.colorImageView.layer.borderColor=[UIColor
greenColor].CGColor;
self.colorImageView.layer.shadowColor=[[UIColor
yellowColor]
CGColor];
self.colorImageView.layer.shadowOffset=CGSizeMake(20,
20);
self.colorImageView.layer.shadowOpacity=0.5;
[self.colorImageView
setClipsToBounds:YES];
self.colorImageView.layer.masksToBounds=NO;
self.secondVC=[self.storyboard
instantiateViewControllerWithIdentifier:@"secondVC"];
}
-(void)testImageView{
//[self.imageView.layer setValue:@(M_PI_4) forKeyPath:@"transform.rotation"];
// NSValue *value=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
// [self.imageView.layer setValue:value forKeyPath:@"transform"];
//[self.imageView.layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)] forKeyPath:@"transform"];
// [self.imageView.layer setValue:[NSNumber numberWithFloat:0.5] forKeyPath:@"transform.scale.y"];
[self.imageView.layer
setValue:@(0.5)
forKeyPath:@"transform.scale.y"];
}
//UIView动画的动画块方法
- (IBAction)propertyAnimation:(UIButton *)sender {
[UIView
beginAnimations:@"AnimationOne"
context:nil];
[UIView
setAnimationDuration:1];
[UIView
setAnimationRepeatCount:2];
//[UIView setAnimationDelegate:self];
这个有什么用??
self.colorImageView.bounds=CGRectMake(120,
80, 100,
100);
self.colorImageView.layer.cornerRadius=50;
self.colorImageView.transform=CGAffineTransformMakeRotation(M_PI);
[UIView
commitAnimations];
}
//UIView动画的block方法
- (IBAction)blockAnimation:(UIButton *)sender {
[UIView
animateWithDuration:1
delay:1
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{
self.colorImageView.bounds=CGRectMake(0,
0, 200,
200);
self.colorImageView.transform=CGAffineTransformMakeRotation(2*M_PI);
} completion:^(BOOL finished) {
}];
}
- (IBAction)transmitAnimation:(UIButton *)sender {
UIView *view=self.view.superview;
[UIView
beginAnimations:@"transmit"
context:nil];
[UIView
setAnimationDuration:3];
[UIView
setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:self.view.superview
cache:YES];
[self.view
removeFromSuperview];
[view addSubview:self.secondVC.view];
[UIView
commitAnimations];
}
//UIViewTransition的API方法
- (IBAction)blockTransmitAnimation:(UIButton *)sender {
[UIView
transitionFromView:self.view
toView:self.secondVC.view
duration:1
options:UIViewAnimationOptionTransitionCurlUp
completion:^(BOOL finished) {
}];
UIView *view=self.view.superview;
[UIView
transitionWithView:view duration:1
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.view
removeFromSuperview];
[view addSubview:self.secondVC.view];
} completion:^(BOOL finished) {
}];
}
//CAAnimation是抽象类,CAPropertyAnimation-->CABasicAnimation
- (IBAction)basicAnimation:(UIButton *)sender {
CABasicAnimation *basicAnimation=[CABasicAnimation
animationWithKeyPath:@"backgroundColor"];
basicAnimation.duration=6;
basicAnimation.fromValue = (__bridge
id)([[UIColor
redColor] CGColor]);
basicAnimation.toValue = (__bridge
id)([[UIColor
purpleColor] CGColor]);
// basicAnimation.fromValue = [NSNumber numberWithFloat:0.5];
// basicAnimation.byValue = [NSNumber numberWithFloat:0.2];
[self.colorImageView.layer
addAnimation:basicAnimation
forKey:@"haha"];
self.colorImageView.backgroundColor=[UIColor
yellowColor];
}
//CAAnimation是抽象类,CAPropertyAnimation-->CAKeyFrameAnimation
- (IBAction)keyFrameAnimation:(UIButton *)sender {
CAKeyframeAnimation *keyFrameAnimation=[CAKeyframeAnimation
animationWithKeyPath:@"position.x"];
CGFloat center=self.colorImageView.layer.position.x;
CGFloat left=center-20;
CGFloat right=center+20;
NSNumber *c=[NSNumber
numberWithFloat:center];
NSNumber *l=[NSNumber
numberWithFloat:left];
NSNumber *r=[NSNumber
numberWithFloat:right];
keyFrameAnimation.values=@[c,r,l,c,l,r,c,r,l,r,c,l];
[self.colorImageView.layer
addAnimation:keyFrameAnimation
forKey:@"shake"];
}
//CATransition,作⽤:layer的过渡动画
//有两个主要属性:type(设置过渡动画的效果)和subType(设置过渡
动画的⽅方向)
- (IBAction)coreAnimation:(UIButton *)sender {
CATransition *transition=[CATransition
animation];
transition.duration=3;
transition.startProgress=0.5;
transition.type=@"cube";
transition.subtype=kCATransitionFromLeft;
UIView *view =
self.view.superview;
[view.layer addAnimation:transition
forKey:@"过渡"];
[self.view
removeFromSuperview];
[view addSubview:self.secondVC.view];
}
//CAAnimation是抽象类,CAAnimationGroup-->CAAnimationGroup只有⼀一个数组属性,可以添加多个
CAAnimation,⼀一起执⾏行
- (IBAction)groupAnimation:(UIButton *)sender {
CAAnimationGroup *groupAnimation=[CAAnimationGroup
animation];
CABasicAnimation *basicAnimation=[CABasicAnimation
animationWithKeyPath:@"position"];
basicAnimation.fromValue=[NSValue
valueWithCGPoint:self.colorImageView.layer.position];
basicAnimation.toValue=[NSValue
valueWithCGPoint:CGPointMake(50,
300)];
CABasicAnimation *basicAnimation1=[CABasicAnimation
animationWithKeyPath:@"color"];
basicAnimation1.fromValue = (__bridge
id)([[UIColor
redColor] CGColor]);
basicAnimation1.toValue = (__bridge
id)([[UIColor
purpleColor] CGColor]);
groupAnimation.duration=3;
groupAnimation.animations=@[basicAnimation,basicAnimation1];
[self.colorImageView.layer
addAnimation:groupAnimation
forKey:@"2"];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// FirstViewController.h
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FirstViewController :
UIViewController
@end
// FirstViewController.m
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import "FirstViewController.h"
#import "Macro.h"
#import "ViewController.h"
@interface
FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
bootAnimation];
}
-(void)bootAnimation{
UIImageView *bootImageView=[[UIImageView
alloc]
initWithFrame:[UIScreen
mainScreen].bounds];
bootImageView.image=[UIImage
imageNamed:@"zhiyan"];
[self.view
addSubview:bootImageView];
[UIView
animateWithDuration:3
delay:0
options:UIViewAnimationOptionOverrideInheritedDuration
animations:^{
bootImageView.frame=CGRectMake(-30, -50,
kScreen_Width+60,
kScreen_Height+100);
} completion:^(BOOL finished) {
[bootImageView removeFromSuperview];
ViewController *viewC=[self.storyboard
instantiateViewControllerWithIdentifier:@"viewVC"];
//[self.view removeFromSuperview];
[self.view
addSubview:viewC.view];
}];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// SecondViewController.h
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import <UIKit/UIKit.h>
extern NSString *
const string;
@interface SecondViewController :
UIViewController
@end
// SecondViewController.m
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import "SecondViewController.h"
#import "ViewController.h"
@interface
SecondViewController ()
@property(nonatomic,retain)
ViewController *viewController;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super
viewDidLoad];
NSLog(@"string :%@",string);
_viewController=[self.storyboard
instantiateViewControllerWithIdentifier:@"viewVC"];
}
- (IBAction)swiftBack:(UIButton *)sender {
[UIView
transitionFromView:self.view
toView:self.viewController.view
duration:1
options:UIViewAnimationOptionTransitionCurlDown
completion:^(BOOL finished) {
}];
UIView *view=self.view.superview;
[UIView
transitionWithView:view duration:1
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
[self.view
removeFromSuperview];
[view addSubview:self.viewController.view];
} completion:^(BOOL finished) {
}];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
// ViewController.h
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController :
UIViewController
@end
// ViewController.m
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
NSString * const string =
@"123";
@interface
ViewController ()
@property (strong,
nonatomic) IBOutlet
UIImageView *imageView;
@property (strong,
nonatomic) IBOutlet
UIImageView *colorImageView;
@property(nonatomic,retain)
SecondViewController *secondVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.imageView.image=[UIImage
imageNamed:@"10042637.jpg"];
self.imageView.layer.cornerRadius=20;
self.imageView.layer.borderWidth=2;
self.imageView.layer.borderColor=[UIColor
greenColor].CGColor;
self.imageView.layer.shadowColor=[[UIColor
blueColor]
CGColor];
self.imageView.layer.shadowOffset=CGSizeMake(20,
20);
self.imageView.layer.shadowOpacity=0.5;
[self.imageView
setClipsToBounds:YES];
//超过主层边框范围的内容都裁剪
self.imageView.layer.masksToBounds=NO;
//[self testImageView];
self.colorImageView.backgroundColor=[UIColor
purpleColor];
self.colorImageView.layer.cornerRadius=20;
self.colorImageView.layer.borderWidth=2;
self.colorImageView.layer.borderColor=[UIColor
greenColor].CGColor;
self.colorImageView.layer.shadowColor=[[UIColor
yellowColor]
CGColor];
self.colorImageView.layer.shadowOffset=CGSizeMake(20,
20);
self.colorImageView.layer.shadowOpacity=0.5;
[self.colorImageView
setClipsToBounds:YES];
self.colorImageView.layer.masksToBounds=NO;
self.secondVC=[self.storyboard
instantiateViewControllerWithIdentifier:@"secondVC"];
}
-(void)testImageView{
//[self.imageView.layer setValue:@(M_PI_4) forKeyPath:@"transform.rotation"];
// NSValue *value=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
// [self.imageView.layer setValue:value forKeyPath:@"transform"];
//[self.imageView.layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)] forKeyPath:@"transform"];
// [self.imageView.layer setValue:[NSNumber numberWithFloat:0.5] forKeyPath:@"transform.scale.y"];
[self.imageView.layer
setValue:@(0.5)
forKeyPath:@"transform.scale.y"];
}
//UIView动画的动画块方法
- (IBAction)propertyAnimation:(UIButton *)sender {
[UIView
beginAnimations:@"AnimationOne"
context:nil];
[UIView
setAnimationDuration:1];
[UIView
setAnimationRepeatCount:2];
//[UIView setAnimationDelegate:self];
这个有什么用??
self.colorImageView.bounds=CGRectMake(120,
80, 100,
100);
self.colorImageView.layer.cornerRadius=50;
self.colorImageView.transform=CGAffineTransformMakeRotation(M_PI);
[UIView
commitAnimations];
}
//UIView动画的block方法
- (IBAction)blockAnimation:(UIButton *)sender {
[UIView
animateWithDuration:1
delay:1
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{
self.colorImageView.bounds=CGRectMake(0,
0, 200,
200);
self.colorImageView.transform=CGAffineTransformMakeRotation(2*M_PI);
} completion:^(BOOL finished) {
}];
}
- (IBAction)transmitAnimation:(UIButton *)sender {
UIView *view=self.view.superview;
[UIView
beginAnimations:@"transmit"
context:nil];
[UIView
setAnimationDuration:3];
[UIView
setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:self.view.superview
cache:YES];
[self.view
removeFromSuperview];
[view addSubview:self.secondVC.view];
[UIView
commitAnimations];
}
//UIViewTransition的API方法
- (IBAction)blockTransmitAnimation:(UIButton *)sender {
[UIView
transitionFromView:self.view
toView:self.secondVC.view
duration:1
options:UIViewAnimationOptionTransitionCurlUp
completion:^(BOOL finished) {
}];
UIView *view=self.view.superview;
[UIView
transitionWithView:view duration:1
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.view
removeFromSuperview];
[view addSubview:self.secondVC.view];
} completion:^(BOOL finished) {
}];
}
//CAAnimation是抽象类,CAPropertyAnimation-->CABasicAnimation
- (IBAction)basicAnimation:(UIButton *)sender {
CABasicAnimation *basicAnimation=[CABasicAnimation
animationWithKeyPath:@"backgroundColor"];
basicAnimation.duration=6;
basicAnimation.fromValue = (__bridge
id)([[UIColor
redColor] CGColor]);
basicAnimation.toValue = (__bridge
id)([[UIColor
purpleColor] CGColor]);
// basicAnimation.fromValue = [NSNumber numberWithFloat:0.5];
// basicAnimation.byValue = [NSNumber numberWithFloat:0.2];
[self.colorImageView.layer
addAnimation:basicAnimation
forKey:@"haha"];
self.colorImageView.backgroundColor=[UIColor
yellowColor];
}
//CAAnimation是抽象类,CAPropertyAnimation-->CAKeyFrameAnimation
- (IBAction)keyFrameAnimation:(UIButton *)sender {
CAKeyframeAnimation *keyFrameAnimation=[CAKeyframeAnimation
animationWithKeyPath:@"position.x"];
CGFloat center=self.colorImageView.layer.position.x;
CGFloat left=center-20;
CGFloat right=center+20;
NSNumber *c=[NSNumber
numberWithFloat:center];
NSNumber *l=[NSNumber
numberWithFloat:left];
NSNumber *r=[NSNumber
numberWithFloat:right];
keyFrameAnimation.values=@[c,r,l,c,l,r,c,r,l,r,c,l];
[self.colorImageView.layer
addAnimation:keyFrameAnimation
forKey:@"shake"];
}
//CATransition,作⽤:layer的过渡动画
//有两个主要属性:type(设置过渡动画的效果)和subType(设置过渡
动画的⽅方向)
- (IBAction)coreAnimation:(UIButton *)sender {
CATransition *transition=[CATransition
animation];
transition.duration=3;
transition.startProgress=0.5;
transition.type=@"cube";
transition.subtype=kCATransitionFromLeft;
UIView *view =
self.view.superview;
[view.layer addAnimation:transition
forKey:@"过渡"];
[self.view
removeFromSuperview];
[view addSubview:self.secondVC.view];
}
//CAAnimation是抽象类,CAAnimationGroup-->CAAnimationGroup只有⼀一个数组属性,可以添加多个
CAAnimation,⼀一起执⾏行
- (IBAction)groupAnimation:(UIButton *)sender {
CAAnimationGroup *groupAnimation=[CAAnimationGroup
animation];
CABasicAnimation *basicAnimation=[CABasicAnimation
animationWithKeyPath:@"position"];
basicAnimation.fromValue=[NSValue
valueWithCGPoint:self.colorImageView.layer.position];
basicAnimation.toValue=[NSValue
valueWithCGPoint:CGPointMake(50,
300)];
CABasicAnimation *basicAnimation1=[CABasicAnimation
animationWithKeyPath:@"color"];
basicAnimation1.fromValue = (__bridge
id)([[UIColor
redColor] CGColor]);
basicAnimation1.toValue = (__bridge
id)([[UIColor
purpleColor] CGColor]);
groupAnimation.duration=3;
groupAnimation.animations=@[basicAnimation,basicAnimation1];
[self.colorImageView.layer
addAnimation:groupAnimation
forKey:@"2"];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// FirstViewController.h
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FirstViewController :
UIViewController
@end
// FirstViewController.m
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import "FirstViewController.h"
#import "Macro.h"
#import "ViewController.h"
@interface
FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
bootAnimation];
}
-(void)bootAnimation{
UIImageView *bootImageView=[[UIImageView
alloc]
initWithFrame:[UIScreen
mainScreen].bounds];
bootImageView.image=[UIImage
imageNamed:@"zhiyan"];
[self.view
addSubview:bootImageView];
[UIView
animateWithDuration:3
delay:0
options:UIViewAnimationOptionOverrideInheritedDuration
animations:^{
bootImageView.frame=CGRectMake(-30, -50,
kScreen_Width+60,
kScreen_Height+100);
} completion:^(BOOL finished) {
[bootImageView removeFromSuperview];
ViewController *viewC=[self.storyboard
instantiateViewControllerWithIdentifier:@"viewVC"];
//[self.view removeFromSuperview];
[self.view
addSubview:viewC.view];
}];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// SecondViewController.h
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import <UIKit/UIKit.h>
extern NSString *
const string;
@interface SecondViewController :
UIViewController
@end
// SecondViewController.m
// Animation_byEpisode
//
// Created by Wangyun on 15/7/9.
// Copyright (c) 2015年 com.epsiode. All rights reserved.
//
#import "SecondViewController.h"
#import "ViewController.h"
@interface
SecondViewController ()
@property(nonatomic,retain)
ViewController *viewController;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super
viewDidLoad];
NSLog(@"string :%@",string);
_viewController=[self.storyboard
instantiateViewControllerWithIdentifier:@"viewVC"];
}
- (IBAction)swiftBack:(UIButton *)sender {
[UIView
transitionFromView:self.view
toView:self.viewController.view
duration:1
options:UIViewAnimationOptionTransitionCurlDown
completion:^(BOOL finished) {
}];
UIView *view=self.view.superview;
[UIView
transitionWithView:view duration:1
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
[self.view
removeFromSuperview];
[view addSubview:self.viewController.view];
} completion:^(BOOL finished) {
}];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
相关文章推荐
- iOS限制此应用不可在越狱机上使用
- IOS 真机调试 There was an internal API error.
- iOS生成同时支持armv7,armv7s,i386的静态库.a文件
- 关于ios8以后BitCode一词,关于在xcode7(bate3)中报错的解决以及普及
- IOS中的常见问题
- 如何解决iOS中更改系统定位权限时候系统设置崩溃的问题
- IOS - 隐式动画
- 【译】关于iOS/OS X线程安全的基础知识
- iOS- 全方位解析.crash文件崩溃报告
- iOS oc基础知识
- IOS学习之蓝牙4.0
- iOS9TableView分割线默认不显示,只有滑动的时候才显示 解决办法
- iOS textfiled限制长度
- iOS 动画效果:Core Animation & Facebook's pop
- IOS NSDate获取的时间比系统时间长1年
- 详解iOS中多线程app开发的GCD队列的使用
- iOS沙盒目录
- iOS 沙盒存储 读取图片
- ios9 如何集成ios9 里面的3D Touch
- IOS----向用户征询iOS授权的五种常见设计模式