OC-UIImage动画&UIImageView动画&UIView动画NSTimer动画

1.UIKit 层面的动画

预备:动画 — 帧动画

1.1UIImage

1.2UIImageView

预备:动画 — 补间动画

1.3UIView

系统为UIView提供的专门用于控制视图实现动画的方法,这些方式以类方法出现的,方法名开头为animate….

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

//    [UIView animateWithDuration:2 animations:^{
//        //设置动画的最终状态
//        self.imageView.alpha = 1;
//        CGPoint center = self.imageView.center;
//        center.y += 300;
//        self.imageView.center = center;
//    }];

//    [UIView animateWithDuration:2 animations:^{
//        self.imageView.alpha = 1;
//    } completion:^(BOOL finished) {
//        //动画执行完 执行 该代码块
//        NSLog(@"动画执行完");
//    }];

    /*
     Duration 动画持续时间
     delay  等待时间
     options 动画选项  (动画匀速 变速 重复)
     animations 动画结束后什么样子 (最终状态)
     completion 动画结束后做什么
    */
     [UIView animateWithDuration:2 delay:3 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
         CGPoint center = self.imageView.center;
         center.y += 300;
         self.imageView.center = center;
     } completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置 动画的初始状态
//    self.imageView.alpha = 0;
}

转场动画:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.imageIndex++;
    if (self.imageIndex > 43) return;

    NSString *imageName = [NSString stringWithFormat:@"a%ld",self.imageIndex];

    //添加转场动画
    [UIView transitionWithView:self.imageView duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
        self.imageView.image = [UIImage imageNamed:imageName];
    } completion:nil];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    //ship-anim 图片名称 但是索引0 不要
    //duration 动画持续时间
    UIImage *image = [UIImage animatedImageNamed:@"ship-anim" duration:1];
    self.imageView.image = image;


    [self runAnimationWithImageView:self.npcImageView imageFileName:@"yinJiaoDaWang" imageCount:32 speed:1/20.0];
//
    [self runAnimationWithImageView:self.npcImageView2 imageFileName:@"shaoNv3_" imageCount:40 speed:1/15.0];

  /******** UIImageView动画 *********************************/
    NSMutableArray *allImages = [NSMutableArray array];
    for (NSInteger i = 0; i < 32; i++) {
        //拼接图片的名称
        NSString *fileName = [NSString stringWithFormat:@"yinJiaoDaWang%02ld",i+1];
        //创建图片对象
        UIImage *image = [UIImage imageNamed:fileName];
        //将创建好的图片 添加到数组中
        [allImages addObject:image];
    }
    //设置 动画所需图片 (需要的是一个图片数组)
    self.npcImageView.animationImages = allImages;
    //设置动画的时长  (一次多长时间)
    self.npcImageView.animationDuration = 1 / 10.0 * 32;
    //设置动画运行次数   值为0 是无限运行
    self.npcImageView.animationRepeatCount = 0;
    //运行动画
    [self.npcImageView startAnimating];

    if (self.npcImageView.animationRepeatCount != 0) {
        //动画运行完 要释放动画数组
        //获取动画总时间
        CGFloat afterDelay = self.npcImageView.animationDuration * self.npcImageView.animationRepeatCount;
        //等待 afterDelay 时间后 向 self.npcImageView 发送setAnimationImages 消息 并把 nil 做为参数传给 setAnimationImages 方法
        [self.npcImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:afterDelay];
    }
}

页面跳转动画:

MyViewController *myVC = [[MyViewController alloc]init];
    /*
    UIModalTransitionStyleCoverVertical
    UIModalTransitionStyleFlipHorizontal
    UIModalTransitionStyleCrossDissolve
    UIModalTransitionStylePartialCurl
     */
    //设置跳转动画的类型
    myVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    [self presentViewController:myVC animated:YES completion:nil];

2.NSTimer动画

  • 创建定时器
    • [NSTimer schedulexxx];
    • [NSTimer timerWithxxx];
  • 销毁定时器
    • [timer invalidate];

动画主要修改frame bounds center alpha等属性

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.imageView.alpha = 0;
}
-(void)timer:(NSTimer *)sender {

    CGRect bounds = self.imageView.bounds;
    bounds.size.width +=5;
    bounds.size.height += 5;
    self.imageView.bounds = bounds;

//    CGRect frame = self.imageView.frame;
//    frame.size.width +=5;
//    frame.size.height += 5;
//    self.imageView.frame = frame;


//    CGPoint center = self.imageView.center;
//    center.y += 5;
//    self.imageView.center = center;

//    self.imageView.alpha += 0.1;
//    if (self.imageView.alpha >= 1) {
//        [sender invalidate]; //销毁定时器
//    }

    NSLog(@"-------");
}
发布了52 篇原创文章 · 获赞 5 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/shuan9999/article/details/52649664