您的位置:首页 > 产品设计 > UI/UE

iOS开发之 UIView动画图,animationView

2015-09-14 16:29 381 查看
效果图就如汤姆猫,这个就不用我展示了把

<span style="font-size:14px;">    UIImageView *animationView;
NSMutableArray *imageList;</span>


<span style="font-size:14px;">  self.view.backgroundColor = [UIColor whiteColor];

[self loadData];

//    UIImage *image = [UIImage imageNamed:@"cymbal_00.jpg"];
animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
//    设置UIImageView播放的动态数组图
animationView.animationImages = imageList;

//    播放一组动画需要的时间
animationView.animationDuration = 10;
////    -1 循环播放
animationView.animationRepeatCount = -1;
//
[self.view addSubview:animationView];

//    [animationView startAnimating];//让动画开始
//    [animationView stopAnimating];//让动画结束
//    animationView.isAnimating //判断动画是否播放

UIButton *b1 = [UIButton buttonWithType:UIButtonTypeCustom];
b1.backgroundColor = [UIColor whiteColor];
b1.frame = CGRectMake(0, 600, 50, 50);
[b1 setTitle:@"开始" forState:UIControlStateNormal];
[b1 setTitle:@"暂停" forState:UIControlStateSelected];

[b1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[b1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

b1.showsTouchWhenHighlighted = YES;

[b1 addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:b1];

NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(moveAnimation) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)loadData
{
//    当调试的时候 打印数组的值 是nil或者null 意味着我们没有初始化对象 或者有被赋值为nil或者null(数组对象就会被销毁)的情况
imageList = [[NSMutableArray alloc]init];
for (int i = 0; i<=25; i++) {
NSString *imageName = [NSString stringWithFormat:@"cymbal_%02d.jpg",i];
[imageList addObject:[UIImage imageNamed:imageName]];
}

NSLog(@"%@",imageList);
}

- (void)play:(UIButton *)sender
{
if (sender.selected != YES) {
//        播放动画 重置按钮的title
sender.selected = YES;
[animationView startAnimating];
[self moveAnimation];

}else{
sender.selected = NO;
[animationView stopAnimating];
}
}

- (void)moveAnimation
{
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(250, 400, 50*2, 50*2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(0 , 0, 375, 667);
}];
}];
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: