UIButton实现控制动画的开始和停止
2016-04-16 17:47
387 查看
import “ViewController.h”
@interface ViewController ()@property(nonatomic,retain)UIImageView *imageView;
@end
@implementation ViewController
- (void)dealloc
{
self.imageView = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor]; UIImage *image = [UIImage imageNamed:@"Zombie0.tiff"]; self.imageView = [[UIImageView alloc]initWithImage:image]; _imageView.frame = CGRectMake(60, 60, 160, 240); [self.view addSubview:_imageView]; [_imageView release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.backgroundColor = [UIColor brownColor]; button.frame = CGRectMake(140, 360, 40, 30); [button setTitle:@"开始" forState:UIControlStateNormal]; [self.view addSubview:button]; //添加关联事件 [button addTarget:self action:@selector(controlAnimation:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)controlAnimation:(UIButton *)sender{
//取出button上的title NSString *title = [sender titleForState:UIControlStateNormal]; //判断title是否和@"开始"相同,相同则开始动画,并修改标题为@"停止",如果不相同,则停止动画,并修改标题为@"开始" if ([title isEqualToString:@"开始"]) { [self aninationWithImageName:@"Zombie"andImageCount:21]; [sender setTitle:@"停止" forState:UIControlStateNormal]; } else { [_imageView stopAnimating]; [sender setTitle:@"开始" forState:UIControlStateNormal]; }
}
(void)aninationWithImageName:(NSString *)names andImageCount:(int )count{
if (_imageView.isAnimating) {
return;
}
NSMutableArray *imageArray = [NSMutableArray array];
for (int i = 0; i < count; i++) {
NSString *name = [NSString stringWithFormat:@"%@%d.tiff",names,i]; UIImage *image = [UIImage imageNamed:name]; [imageArray addObject:image]; _imageView.animationImages = imageArray; _imageView.animationDuration = imageArray.count * 0.03; _imageView.animationRepeatCount = 0; [_imageView startAnimating];
}
}
相关文章推荐
- iOS---UIViewContentMode各类型效果
- sublime text2 build system 执行带中文路经文件
- iOS UITextField修改信息
- PHP-include和require
- 【问题】Failed to send producer request with correlation id 2 to broker 0 with
- UVALive 6195 —— The Dueling Philosophers Problem
- Map很多Key-Value对应是可以此来处理!
- UITableView 总结
- 界面编程与视图(view)组件——三种UI界面开发方式
- Android设计模式之建造者模式(Builder Pattern)
- Android问题集锦之二十九:Cannot run program "/xx/build-tools/xx/aapt": error=2, No such file or directory
- 核心动画的简单理解
- 限制一个UITextField的输入字数
- Android issues
- java之Symmetric encryption techniques
- IOS开发之UIView总结
- Jersey(1.19.1) - Building Responses
- ngui中scrollview的使用
- 解决sui的Form表单验证回调问题
- UI 开发