iphone 利用UIImageView來製作投影片

來源:互聯網
上載者:User

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

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.