标签:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. /*********UIImage***********/ //由名字直接读取图片 //优点:用时相对较短 //缺点:读取之后会占用内存 //如果图片较小,用名字直接读取 UIImage *imageName = [UIImage imageNamed:@"1.png"]; //UIImageView UIImageView *imageViewName = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 60, 60)]; imageViewName.image = imageName; [self.view addSubview:imageViewName]; //由图片的路径读取图片 //优点:读取之后不会占用内存 //缺点:用时相对较长 //如果图片较大,用路径直接读取 //获取图片路径:相对路径 //第一个参数:文件的名字 //第二个参数:文件的类型 NSString *path = [[NSBundle mainBundle] pathForResource:@"map" ofType:@"png"]; UIImage *imagePath = [UIImage imageWithContentsOfFile:path]; //UIImageView UIImageView *imageViewPath = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, 200, 50)]; imageViewPath.image = imagePath; [self.view addSubview:imageViewPath]; /**********UIImageView-动画效果*************/ UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 240, 60, 60)]; imageView.backgroundColor = [UIColor grayColor]; imageView.image = [UIImage imageNamed:@"1.png"]; /****动画效果相关属性****/ //创建图片数组 //开辟内存空间 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0]; for (int i = 1; i <= 12; i ++) { [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"player%d.png",i]]]; } //设置动画图片,需要接收一个数组,数组里面的类型必须是UIImage类型 imageView.animationImages = array; //动画时间:数组里面所有的图片转一圈所用时间 imageView.animationDuration = 1; //循环次数:大于0的数:写几就循环几次,结束 0:无限循环 imageView.animationRepeatCount = 0; //tag imageView.tag = 1000; [self.view addSubview:imageView]; //开始动画 [imageView startAnimating]; //UIButton UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(30, 330, 260, 30); button.backgroundColor = [UIColor redColor]; [button setTitle:@"button" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } #pragma mark - 按钮的点击事件:停止或开始动画 - (void)buttonClick:(UIButton *)button{ //找到UIImageView UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1000]; //停止动画 // [imageView stopAnimating]; static BOOL isAnimation = YES; if (isAnimation) { [imageView stopAnimating]; isAnimation = NO; }else{ [imageView startAnimating]; isAnimation = YES; } }
//*********************************************
//时间 //第一个参数执行动作相隔的时间 //第二个参数通知给谁:self //第三个参数:执行的相关方法 //第四个参数:nil //第五个参数:是否重复执行 YES:重复 NO:不重复 // [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer:) userInfo:nil repeats:YES]; //self.view.bounds 以(0, 0)点为起点,全屏大小的view UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.image = [UIImage imageNamed:@"back2.jpg"]; [self.view addSubview:imageView]; //动画效果的UIImageView UIImageView *birdView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 30, 60.5, 48)]; //tag birdView.tag = 1000; //图片数组 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0]; for (int i = 1; i <= 18; i ++) { [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"DOVE%d.png",i]]]; } /**UIImageView的动画属性***/ birdView.animationImages = array; birdView.animationDuration = 1; birdView.animationRepeatCount = 0; //开始动画 [birdView startAnimating]; [imageView addSubview:birdView]; //控制bird位置 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeFrame:) userInfo:nil repeats:YES]; } #pragma mark - 改变bird的位置 - (void)changeFrame:(NSTimer *)timer{ UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1000]; static int i = 0; [UIView animateWithDuration:1 animations:^{ imageView.frame = CGRectMake(5 + (i++ * 10), 20 +arc4random()%30, 60.5, 48); }]; if (5 +(i++ * 10) > 320) { imageView.frame = CGRectMake(5, 20, 60.5, 48); i = 0; } } #pragma mark - NSTimer的事件 - (void)timer:(NSTimer *)timer{ NSLog(@"timer"); } #pragma mark - 按钮的点击事件 - (void)buttonClick:(UIButton *)button{ //UIView的动画效果 //第一个参数:动画执行的时间 //第二个参数:执行动作 // [UIView animateWithDuration:3 animations:^{ // self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]; // }]; //第一个参数:动画执行的时间 //第二个参数:动画延迟执行的时间 //第三个参数:动画执行的效果 //第四个参数:执行动作 //第五个参数:执行完要做的动作之后做的操作 [UIView animateWithDuration:3 delay:1 options:UIViewAnimationOptionOverrideInheritedOptions animations:^{ self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]; } completion:^(BOOL finished) { // self.view.backgroundColor = [UIColor orangeColor]; }]; }
标签:
原文地址:http://www.cnblogs.com/hyuganatsu/p/UIImageView.html