UIImageView

时间:2023-03-09 06:57:47
UIImageView
- (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(, , , )];
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(, , , )];
imageViewPath.image = imagePath;
[self.view addSubview:imageViewPath]; /**********UIImageView-动画效果*************/
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
imageView.backgroundColor = [UIColor grayColor];
imageView.image = [UIImage imageNamed:@"1.png"]; /****动画效果相关属性****/
//创建图片数组
//开辟内存空间
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:];
for (int i = ; i <= ; i ++) {
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"player%d.png",i]]];
} //设置动画图片,需要接收一个数组,数组里面的类型必须是UIImage类型
imageView.animationImages = array;
//动画时间:数组里面所有的图片转一圈所用时间
imageView.animationDuration = ;
//循环次数:大于0的数:写几就循环几次,结束 0:无限循环
imageView.animationRepeatCount = ; //tag
imageView.tag = ; [self.view addSubview:imageView]; //开始动画
[imageView startAnimating]; //UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(, , , );
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:];
//停止动画
// [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(, , 60.5, )];
//tag
birdView.tag = ; //图片数组
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:];
for (int i = ; i <= ; i ++) {
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"DOVE%d.png",i]]];
} /**UIImageView的动画属性***/
birdView.animationImages = array;
birdView.animationDuration = ;
birdView.animationRepeatCount = ;
//开始动画
[birdView startAnimating]; [imageView addSubview:birdView]; //控制bird位置
[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(changeFrame:) userInfo:nil repeats:YES];
}
#pragma mark - 改变bird的位置
- (void)changeFrame:(NSTimer *)timer{
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:];
static int i = ;
[UIView animateWithDuration: animations:^{
imageView.frame = CGRectMake( + (i++ * ), +arc4random()%, 60.5, );
}];
if ( +(i++ * ) > ) {
imageView.frame = CGRectMake(, , 60.5, );
i = ;
}
}
#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: delay: options:UIViewAnimationOptionOverrideInheritedOptions animations:^{
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%/255.0 green:arc4random()%/255.0 blue:arc4random()%/255.0 alpha:1.0];
} completion:^(BOOL finished) {
// self.view.backgroundColor = [UIColor orangeColor];
}];
}