iphone 利用UIImageView来制作幻灯片

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
 

 

 

posted on 2011-12-14 21:12  wtq  阅读(926)  评论(0编辑  收藏  举报