简单动态相册,
相册功能很实用, 属于必须掌握的范畴.
准备:
图片若干张;
创建UIView
创建UIViewController
实现:
点击 start按钮, 开始动态显示
点击 stop按钮, 停止
UIView中设置属性, 并实现
@property (nonatomic, retain)UIImageView *imageView;
// 开始按钮
@property (nonatomic, retain)UIButton *startButton;
// 停止按钮
@property (nonatomic, retain)UIButton *stopButton;
实现
- (void)dealloc
{
[_startButton release];
[_stopButton release];
[_imageView release];
[super dealloc];
}
// 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubviews];
}
return self;
}
// 视图布局
- (void)addSubviews
{
// 创建imageView
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 300)/2, 50, 300, 460)];
self.imageView.backgroundColor = [UIColor redColor];
// 添加图片
UIImage *imagee = [UIImage imageNamed:@"13.jpg"];
self.imageView.image = imagee;
[self addSubview:self.imageView];
[_imageView release];
// 创建开始按钮
self.startButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
self.startButton.frame = CGRectMake(self.imageView.left, self.imageView.bottom + 20, 80, 40);
self.startButton.backgroundColor = [UIColor grayColor];
[self addSubview:self.startButton];
//创建停止按钮
self.stopButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.stopButton.frame = CGRectMake(self.imageView.right - 80, self.imageView.bottom + 20, 80, 40);
self.stopButton.backgroundColor = [UIColor grayColor];
[self addSubview:self.stopButton];
}
进入控制器实现, 先导入头文件
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
SJView *sjview = [[SJView alloc] initWithFrame:[UIScreen mainScreen].bounds];
sjview.backgroundColor = [UIColor purpleColor];
[self.view addSubview:sjview];
[sjview release];
// 创建图片数组
NSMutableArray *imageArray = [NSMutableArray array];
// 循环添加图片 图片名字从1开始
for (int i = 1; i < 6; i++) {
NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i];
NSLog(@"%@", imageName);
// 循环图片
UIImage *image = [UIImage imageNamed:imageName];
// 添加到数组中
[imageArray addObject:image];
}
// 设置播放图片的数组
sjview.imageView.animationImages = imageArray;
// 设置播放每张间隔
sjview.imageView.animationDuration = 6;
// 设置循环播放几次
sjview.imageView.animationRepeatCount = 0;
// 给button添加方法
[sjview.startButton addTarget:self action:@selector(actionStartButton:) forControlEvents:(UIControlEventTouchUpInside)];
// 添加字体,开始
[sjview.startButton setTitle:@"GO" forState:(UIControlStateNormal)];
// 字体颜色改变
[sjview.startButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
// 给停止按钮 添加方法
[sjview.stopButton addTarget:self action:@selector(actionStopButton:) forControlEvents:(UIControlEventTouchUpInside)];
// 添加字体,停止
[sjview.stopButton setTitle:@"STOP" forState:(UIControlStateNormal)];
// 加TAG值 方便取出
sjview.imageView.tag = 100;
}
// 停止按钮
- (void)actionStopButton:(UIButton *)buttonn
{
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
[imageView stopAnimating];
NSLog(@"ss");
// 停到指定图片
UIImage *ima = [UIImage imageNamed:@"11.jpg"];
imageView.image = ima;
}
// 实现开始按钮
- (void)actionStartButton:(UIButton *)button
{
// 先取父视图
UIView *supperView = button.superview;
// 用父视图取子视图的数组
NSArray *subViews = supperView.subviews;
// 遍历数组 判断类的类型 取出你想要的
for (UIView *view in subViews) {
// 如果这个类 是UIImageView 就走这个方法
if ([view isKindOfClass:[UIImageView class]]) {
UIImageView *imageVIew = (UIImageView *)view;
[imageVIew startAnimating];
}
}
}
别忘记设置下面的, 在AppDelegate.m中
RootViewController *rootVC = [[RootViewController alloc] init];
self.window.rootViewController = rootVC;
[rootVC release];
over