1:原文摘自:http://www.mobiletrain.org/lecture/doc/iphone/2011-10/735.html
在 iPhone 應用裡加入全屏動畫可以讓應用更具趣味性,以下這段代碼可以實現這一功能
AnimationDemoViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
//指定ImageView的展示地區
UIImageView *fishAni=[UIImageView alloc] initWithFrame:[UIScreen mainScreen] bounds];
//將指定的圖片載入至 animationImages
可以利用NSFileManage從檔案夾中讀取圖片,並顯示。。
fishAni.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:@"1.jpg"],
[UIImage imageNamed:@"2.jpg"],
[UIImage imageNamed:@"3.jpg"],
[UIImage imageNamed:@"4.jpg"],
[UIImage imageNamed:@"5.jpg"],nil ];
//設定動畫播放時間
fishAni.animationDuration=1.0;
//設定重複播放次數,0 為不斷重複
fishAni.animationRepeatCount=0;
//開始播放動畫
[fishAni startAnimating];
//將ImageView 增加到 self.view 的 subview
[self.view addSubview:fishAni];
}
思路:可添加兩種按鈕,一個是暫停動畫按鈕,一個是開始動畫按鈕。
代碼如下:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIImageView *fishAni;
UIButton *startAnimationBtn;
UIButton *endAnimationBtn;
}
-(void) startAnimationBtnClick:(id)sender;
-(void) endAnimationBtnClick:(id)sender;
@end
實現檔案:
#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//建立一個按鈕
startAnimationBtn= [UIButton buttonWithType:UIButtonTypeRoundedRect ];
startAnimationBtn.frame = CGRectMake(20,400, 40, 30);
//startAnimationBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 280, 40, 30)];
[startAnimationBtn setTitle:@"Start" forState:UIControlStateNormal];
[startAnimationBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[startAnimationBtn setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal];
[startAnimationBtn addTarget:self action:@selector(startAnimationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//停止動畫
endAnimationBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
endAnimationBtn.frame = CGRectMake(120, 280, 40, 30);
// endAnimationBtn = [[UIButton alloc] initWithFrame:CGRectMake(120, 280, 40, 30)];
[endAnimationBtn setTitle:@"End" forState:UIControlStateNormal];
[endAnimationBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[endAnimationBtn setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal];
[endAnimationBtn addTarget:self action:@selector(endAnimationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//制定ImageView的展示地區
fishAni = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//將制定的圖片載入至 animationImages
fishAni.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"xiaomao1.jpg"],
[UIImage imageNamed:@"xiaomao2.jpg"],
[UIImage imageNamed:@"xiaomao3.jpg"],
[UIImage imageNamed:@"xiaomao4.jpg"],nil];
//設定動畫播放時間
fishAni.animationDuration = 3.0;
fishAni.animationRepeatCount = 0;
fishAni.highlighted = YES;
[fishAni startAnimating];
[self.view addSubview:fishAni];
[self.view addSubview:startAnimationBtn];
[self.view addSubview:endAnimationBtn];
}
-(void) startAnimationBtnClick:(id)sender
{
[fishAni startAnimating];
}
-(void) endAnimationBtnClick:(id)sender
{
[fishAni stopAnimating];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end