IOS簡易動畫

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

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

UIView *magentaView = [[UIView alloc] initWithFrame:self.view.bounds];

magentaView.backgroundColor = [UIColor magentaColor];

[self.view addSubview:magentaView];

NSLog(@"====%@",[self.view subviews]);

UIView* grayView = [[UIView alloc] initWithFrame:self.view.bounds];

grayView.backgroundColor = [UIColor lightGrayColor];

[self.view addSubview:grayView];

NSArray* bnTitleArray = [NSArray arrayWithObjects:

@"添加" , @"翻頁" , @"移入" , @"揭開" ,

@"立方體" , @"收縮" , @"翻轉(zhuǎn)" , @"水波" , nil];

NSMutableArray* bnArray = [[NSMutableArray alloc] init];

// 獲取屏幕的內(nèi)部高度

CGFloat totalHeight = [UIScreen mainScreen].bounds.size.height;

// 創(chuàng)建8個按鈕,并將按鈕添加到NSMutableArray集合中

for (int i = 0 ; i < 8 ; i++)

{

UIButton* bn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[bn setTitle:[bnTitleArray objectAtIndex:i]

forState:UIControlStateNormal];

NSInteger row = i / 4;

NSInteger col = i % 4;

bn.frame = CGRectMake(5 + col * 80

, totalHeight - (2 - row) * 45 - 20 , 70 , 35);

[self.view addSubview:bn];

[bnArray addObject:bn];

}

// 為8個按鈕分別綁定不同的事件處理方法

[[bnArray objectAtIndex:0] addTarget:self action:@selector(add:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:1] addTarget:self action:@selector(curl:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:2] addTarget:self action:@selector(move:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:3] addTarget:self action:@selector(reveal:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:4] addTarget:self action:@selector(cube:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:5] addTarget:self action:@selector(suck:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:6] addTarget:self action:@selector(oglFlip:)

forControlEvents:UIControlEventTouchUpInside];

[[bnArray objectAtIndex:7] addTarget:self action:@selector(ripple:)

forControlEvents:UIControlEventTouchUpInside];

NSLog(@"~~~~~~~%@" , [[self.view.subviews objectAtIndex:2] backgroundColor]);

NSLog(@"~~~~~~~%@" , [[self.view.subviews objectAtIndex:3] backgroundColor]);

}

-(void) add:(id)sender

{

// 開始執(zhí)行動畫

[UIView beginAnimations:@"animation" context:nil];

[UIView setAnimationDuration:3.0f];

// 控制UIView內(nèi)過渡動畫的類型

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown

forView:self.view cache:YES];

// 設(shè)置動畫的變化曲線

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

// 提交動畫

[UIView commitAnimations];

}

-(void) curl:(id)sender

{

// 開始執(zhí)行動畫

[UIView beginAnimations:@"animation" context:nil];

[UIView setAnimationDuration:1.0f];

// 控制UIView內(nèi)過渡動畫的類型

[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp

forView:self.view cache:YES];

// 設(shè)置動畫的變化曲線

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

// 交換視圖控制器所顯示的UIView中中2個子控件位置

[self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];

[UIView commitAnimations];

}

-(void) move:(id)sender

{

CATransition *transition = [CATransition animation];

transition.duration = 2.0f;

// 使用kCATransitionMoveIn動畫

transition.type = kCATransitionMoveIn;

// 指定動畫方向,從左向右

transition.subtype = kCATransitionFromLeft;

[self.view.layer addAnimation:transition forKey:@"animation"];

[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

}

-(void) reveal:(id)sender

{

CATransition *transition = [CATransition animation];

transition.duration = 2.0f;

// 使用kCATransitionReveal動畫

transition.type = kCATransitionReveal;

// 指定動畫方向,從上到下

transition.subtype = kCATransitionFromTop;

[self.view.layer addAnimation:transition forKey:@"animation"];

// 交換視圖控制器所顯示的UIView中中2個子控件位置

[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

}

-(void) cube:(id)sender

{

CATransition *transition = [CATransition animation];

transition.duration = 2.0f;

// 使用@"cube"動畫

transition.type = @"cube";

// 指定動畫方向,從左到右

transition.subtype = kCATransitionFromLeft;

[self.view.layer addAnimation:transition forKey:@"animation"];

// 交換視圖控制器所顯示的UIView中中2個子控件位置

[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

}

-(void) suck:(id)sender

{

CATransition *transition = [CATransition animation];

transition.duration = 2.0f;

// 使用@"suck"動畫, 該動畫與動畫方向無關(guān)

transition.type = @"suckEffect";

[self.view.layer addAnimation:transition forKey:@"animation"];

// 交換視圖控制器所顯示的UIView中中2個子控件位置

[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

}

-(void) oglFlip:(id)sender

{

CATransition *transition = [CATransition animation];

transition.duration = 0.5f;

// 使用@"oglFlip"動畫

transition.type = @"oglFlip";

// 指定動畫方向為上下翻轉(zhuǎn)

transition.subtype = kCATransitionFromLeft;

[self.view.layer addAnimation:transition forKey:@"animation"];

// 交換視圖控制器所顯示的UIView中中2個子控件位置

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:3];

}

-(void) ripple:(id)sender

{

CATransition *transition = [CATransition animation];

transition.duration = 2.0f;

// 使用@"rippleEffect"動畫,該動畫與方向無關(guān)

transition.type = @"rippleEffect";

[self.view.layer addAnimation:transition forKey:@"animation"];

// 交換視圖控制器所顯示的UIView中中2個子控件位置

[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容