使用UIImageView实现动画

#import “ViewController.h"

@interface ViewController ()

{

       UIImageView *animationView;

       NSMutableArray *imageList;

}

@end

@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    [self loadData]; 


//    UIImage *image =[UIImage imageNamed:@"a-1.tiff"];//还未放入图片

    animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    animationView.animationImages = imageList;

    

//    设置UIImageView动态图数组:

    animationView.animationDuration = 1; // 播放一组动画需要的时间

    animationView.animationRepeatCount = -1; // 是否需要重复播放

    [self.view addSubview:animationView];

    [animationView startAnimating]; // 开始动画

    [animationView stopAnimating]; // 结束动画

//       animationView.isAnimating //判断是否正在播放

    

    UIButton * button = [UIButton  buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(10, 20, 50, 35);

    [button setTitle:@"播放" forState:UIControlStateNormal];

    [button setTitle:@"暂停" forState:UIControlStateSelected];

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

    [self.view addSubview:button];

}

//加载数据的方法

- (void)loadData{

    imageList = [NSMutableArray array]; 

//    当调试的时候 打印数组的值 是nil或者null 意味着 咋们没有初始化对象 或者赋值为空

    for (int i = 1; i <= 33; i++) {

        NSString *imageName = [NSString stringWithFormat:@“dog.jpg-%d(被拖移).tiff”,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 startAnimating];

    }

}

- (void)moveAnimation

{

//设置动画的持续时间和位置

//    [UIView  animateWithDuration:5 animations:

//    ^{

//        animationView.frame = CGRectMake(300, 400, self.view.bounds.size.width, self.view.bounds.size.height) ;

//    }];

    [UIView animateWithDuration:5 animations:^{

        animationView.frame = CGRectMake(0, 400, self.view.bounds.size.width*2, self.view.bounds.size.height*2) ;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:5 animations:^{

            animationView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) ;

        }];

    }];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值