欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Animation

程序员文章站 2024-01-19 13:38:40
...

Animation

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];
    }
  • 封装
#define SPEED 1/15.0
//扔派动画
- (IBAction)pieButtonClick:(id)sender {
    [self runAnimationWithImageView:self.tomImageView imageFileName:@"pie_" imageCount:24 speed:SPEED];
}
-(void)runAnimationWithImageView:(UIImageView*)imageView imageFileName:(NSString*)imageFileName imageCount:(NSInteger)imageCount speed:(CGFloat)speed {
    //判断imageView 是否正在运行动画
    if (imageView.isAnimating) return;
    NSMutableArray *allImages = [NSMutableArray array];
    for (NSInteger i = 0; i < imageCount; i++) {
        //拼接图片的名称
        NSString *fileName = [NSString stringWithFormat:@"%@%02ld",imageFileName, i];
        //创建图片对象
        UIImage *image = [UIImage imageNamed:fileName];
        //将创建好的图片 添加到数组中
        [allImages addObject:image];
    }
    //设置 动画所需图片 (需要的是一个图片数组)
    imageView.animationImages = allImages;
    //设置动画的时长  (一次多长时间)
    imageView.animationDuration = speed * imageCount;
    //设置动画运行次数   值为0 是无限运行
    imageView.animationRepeatCount = 1;
    //运行动画
    [imageView startAnimating];
}

UIView

-(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];
}
  • UIView转场动画
-(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)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    MyViewController *myVC = [[MyViewController alloc]init];
    /*
    UIModalTransitionStyleCoverVertical
    UIModalTransitionStyleFlipHorizontal
    UIModalTransitionStyleCrossDissolve
    UIModalTransitionStylePartialCurl
     */
    //设置跳转动画的类型
    myVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;

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