iOS大典之动态相册
2015-09-28 20:32
543 查看
简单动态相册,
相册功能很实用, 属于必须掌握的范畴.
准备:
图片若干张;
创建UIView
创建UIViewController
实现:
点击 start按钮, 开始动态显示
点击 stop按钮, 停止
UIView中设置属性, 并实现
实现
进入控制器实现, 先导入头文件
别忘记设置下面的, 在AppDelegate.m中
over
相册功能很实用, 属于必须掌握的范畴.
准备:
图片若干张;
创建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
相关文章推荐
- JS 相册效果 自动播放[本地整合]
- jquery实现相册一下滑动两次的方法
- 用dom+xhtml+css制作的一个相册效果代码打包下载
- jQuery实现带滚动导航效果的全屏滚动相册实例
- js+css实现上下翻页相册代码分享
- android照相、相册获取图片剪裁报错的解决方法
- Android相册效果(使用C#和Java分别实现)
- CALayer与UIView的关系
- UIView的bounds和frame区别
- 相册cache目录
- uiview
- uiview生命周期
- UIView翻译 (参考)
- 规范之力:正确使用UIViewController(1)
- 规范之力:正确使用UIViewController(2)
- Android高手进Android ViewPager控件的使用(基于ViewPager的横向相册)!!!
- iOS 转变UIView到UIImage
- iOS5中UIViewController的新方法
- UIView动画
- android调用系统相机与系统相册