iOS - UIImageView 动画

简介: 1、UIImageView 动画1.1 播放图片集播放图片集 @property (nonatomic, strong) UIImageView *playImageView; self.

1、UIImageView 动画

1.1 播放图片集

  • 播放图片集

        @property (nonatomic, strong) UIImageView *playImageView;
    
        self.playImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:self.playImageView];
    
        // 创建图片集
        NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:0];
    
        for (int i = 1; i < 30; i++) {
    
            // 添加图片
            [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]];
        }
    
        // 播放图片集
    
        self.playImageView.animationImages = imageArray;    // 设置播放的图片集(需将图片添加到数组 imageArray 中)
        self.playImageView.animationDuration = 29;          // 设置播放整个图片集的时间
        self.playImageView.animationRepeatCount = 0;        // 设置循环播放次数,默认为 0 一直循环
        [self.playImageView startAnimating];                // 开始播放
    
        // [self.playImageView stopAnimating];              // 停止播放动画
    • 效果

      animation68

1.2 汤姆猫

  • 汤姆猫

        #import <AudioToolbox/AudioToolbox.h>
    
        @property (nonatomic, strong) UIImageView *playImageView;
    
        // 创建播放视图
        self.playImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        self.playImageView.image = [UIImage imageNamed:@"background.jpg"];
        [self.view addSubview:self.playImageView];
    
        // 创建功能按钮
        const CGFloat viewWith = self.view.bounds.size.width;
        const CGFloat viewHeight = self.view.bounds.size.height;
    
        const CGFloat gap = 10;
        const CGFloat buttonWith = self.view.bounds.size.width / 5;
        const CGFloat buttonHeight = buttonWith;
    
        // 功能按钮图片集
        NSArray *buttonImageNameArray = @[@"fart.png", @"cymbal.png", @"drink.png", @"eat.png", @"pie.png", @"scratch.png"];
    
        for (int i = 0; i < 11; i++) {
    
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.playImageView addSubview:button];
    
            self.playImageView.userInteractionEnabled = YES;
    
            if (i < 6) {
    
                // 两边功能按钮的布局
    
                if (i < 3) {
    
                    button.frame = CGRectMake(gap, viewHeight / 2 + (buttonHeight + gap ) * (i % 3), buttonWith, buttonHeight);
                }
                else {
    
                    button.frame = CGRectMake(viewWith - buttonWith - gap, 
                                              viewHeight / 2 + (buttonHeight + gap) * (i % 3), 
                                              buttonWith, 
                                              buttonHeight);
                }
    
                [button setBackgroundImage:[UIImage imageNamed:buttonImageNameArray[i]] forState:UIControlStateNormal];
    
            } else {
    
                // 隐藏按钮的布局
    
                if (i == 6){            // 头
    
                    button.frame = CGRectMake(viewWith/4, viewHeight/5, viewWith/2, viewHeight/4);
                }
                else if (i == 7){       // 肚子
    
                    button.frame = CGRectMake(viewWith/3, viewHeight/3*2, viewWith/3, viewHeight/7);
                }
                else if (i == 8){       // 左脚
    
                    button.frame = CGRectMake(viewWith/4*2, viewHeight/6*5, viewWith/6, viewHeight/7);
                }
                else if (i == 9){       // 右脚
    
                    button.frame = CGRectMake(viewWith/4, viewHeight/6*5, viewWith/5, viewHeight/7);
                }
                else{                   // 尾巴
    
                    button.frame = CGRectMake(viewWith/9*6, viewHeight/7*5, viewWith/7, viewHeight/5);
                }
    
                // button.backgroundColor = [UIColor yellowColor];
            }
    
            button.tag = 100 + i;
    
            // 设置按钮事件
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    
        // 点击按钮事件处理
        - (void)buttonClick:(UIButton *)button {
    
            switch (button.tag - 100) {
    
                case 0:     // fart     放屁
    
                    [self playAnimation:@"fart"];
                    [self performSelector:@selector(playVoice:) withObject:@"fart" afterDelay:0.5];
    
                    break;
    
                case 1:     // cymbal   敲锣
    
                    [self playAnimation:@"cymbal"];
                    [self performSelector:@selector(playVoice:) withObject:@"cymbal" afterDelay:0.5];
    
                    break;
    
                case 2:     // drink    喝牛奶
    
                    [self playAnimation:@"drink"];
                    [self performSelector:@selector(playVoice:) withObject:@"drink" afterDelay:0.5];
    
                    break;
    
                case 3:     // eat      吃小鸟
    
                    [self playAnimation:@"eat"];
                    [self performSelector:@selector(playVoice:) withObject:@"eat" afterDelay:0.5];
    
                    break;
    
                case 4:     // pie      撇东西
    
                    [self playAnimation:@"pie"];
                    [self performSelector:@selector(playVoice:) withObject:@"pie" afterDelay:0.5];
    
                    break;
    
                case 5:     // scratch  抓屏幕
    
                    [self playAnimation:@"scratch"];
                    [self performSelector:@selector(playVoice:) withObject:@"scratch" afterDelay:1.5];
    
                    break;
    
                case 6:     // knockout  头
    
                    [self playAnimation:@"knockout"];
                    [self performSelector:@selector(playVoice:) withObject:@"knockout" afterDelay:0.5];
    
                    break;
    
                case 7:     // stomach  肚子
    
                    [self playAnimation:@"stomach"];
                    [self performSelector:@selector(playVoice:) withObject:@"stomach" afterDelay:0.5];
    
                    break;
    
                case 8:     // foot_left  左脚
    
                    [self playAnimation:@"foot_left"];
                    [self performSelector:@selector(playVoice:) withObject:@"foot_left" afterDelay:0.5];
    
                    break;
    
                case 9:     // foot_right  右脚
    
                    [self playAnimation:@"foot_right"];
                    [self performSelector:@selector(playVoice:) withObject:@"foot_right" afterDelay:0.5];
    
                    break;
    
                case 10:    // angry    尾巴
    
                    [self playAnimation:@"angry"];
                    [self performSelector:@selector(playVoice:) withObject:@"angry" afterDelay:0.8];
    
                    break;
    
                default:
                    break;
            }
        }
    
        // 播放动画
        - (void)playAnimation:(NSString *)key {
    
            // 读取 plist 文件获取图片数量
            NSDictionary *imageNumDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] 
                                                                          pathForResource:@"TomCat" ofType:@"plist"]];
    
            int imageNum = [[imageNumDictionary objectForKey:key] intValue];
    
            NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:0];
    
            for (int i = 0; i < imageNum; i++) {
    
                [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%.2d.jpg", key, i]]];
            }
    
            self.playImageView.animationImages = imageArray;
            self.playImageView.animationDuration = imageNum/13;
            self.playImageView.animationRepeatCount = 1;
    
            [self.playImageView startAnimating];                                     // 播放动画
        }
    
        // 播放声音
        - (void)playVoice:(NSString *)key {
    
            // 添加声音
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                                                        pathForResource:key ofType:@"wav"]]), 
                                                                 &soundID);
    
            AudioServicesPlayAlertSound(soundID);                                   // 播放声音
        }
    • 效果

      animation69animation70

目录
相关文章
|
iOS开发
iOS 动画绘制圆形
iOS 动画绘制圆形
79 1
|
编译器 iOS开发 异构计算
读iOS核心动画笔记
读iOS核心动画笔记
53 0
|
6天前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
20 1
|
15天前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
40 5
|
2月前
|
Swift iOS开发 UED
揭秘一款iOS应用中令人惊叹的自定义动画效果,带你领略编程艺术的魅力所在!
【9月更文挑战第5天】本文通过具体案例介绍如何在iOS应用中使用Swift与UIKit实现自定义按钮动画,当用户点击按钮时,按钮将从圆形变为椭圆形并从蓝色渐变到绿色,释放后恢复原状。文中详细展示了代码实现过程及动画平滑过渡的技巧,帮助读者提升应用的视觉体验与特色。
60 11
|
3月前
|
Swift iOS开发 UED
【绝妙创意】颠覆你的视觉体验!揭秘一款iOS应用中令人惊叹的自定义动画效果,带你领略编程艺术的魅力所在!
【8月更文挑战第13天】本文通过一个具体案例,介绍如何使用Swift与UIKit在iOS应用中创建独特的按钮动画效果。当按钮被按下时,其形状从圆形变化为椭圆形,颜色则从蓝色渐变为绿色;释放后,动画反向恢复原状。利用UIView动画方法及弹簧动画效果,实现了平滑自然的过渡。通过调整参数,开发者可以进一步优化动画体验,增强应用的互动性和视觉吸引力。
49 7
|
iOS开发
iOS 常用阅读软件打开书籍的转场动画
iOS 常用阅读软件打开书籍的转场动画
93 0
|
6月前
|
iOS开发
iOS设备功能和框架: 如何使用 Core Animation 创建动画效果?
iOS设备功能和框架: 如何使用 Core Animation 创建动画效果?
135 0
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
91 0
|
iOS开发
iOS - 个人中心果冻弹性下拉动画
iOS - 个人中心果冻弹性下拉动画
252 0
iOS - 个人中心果冻弹性下拉动画