iOS - UIImageView 动画

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];              // 停止播放动画
    • 效果

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);                                   // 播放声音
        }
    • 效果

时间: 2024-08-01 23:31:17

iOS - UIImageView 动画的相关文章

ios-IOS UIImageView动画效果

问题描述 IOS UIImageView动画效果 怎么实现 UIImageView用一张图片,从指定位置逐渐变大,最后停止的动画 解决方案 // 参数1 完成动画的时间 // 参数2 要完成的动画 // 参数3 该block 写 动画结束后要干的事 UIView animateWithDuration:@"参数1" animations: @"参数2" completion:^(BOOL finished)completion 首先给一个UIImageView初始位

iOS UIView动画详解(Objective-C)

    我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置

【iOS开发】30多个iOS常用动画,带详细注释

30多个iOS常用动画,带详细注释 // // CoreAnimationEffect.h // CoreAnimationEffect // // Created by VincentXue on 13-1-19. // Copyright (c) 2013年 VincentXue. All rights reserved. // import   @interface CoreAnimationEffect : NSObject pragma mark - Custom Animation

ios-如何设置UIImageView动画?

问题描述 如何设置UIImageView动画? 如何设置UIImageView有动画?用了下面的代码进行动画设置,但是对imageView没有用: MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)]; MyImage.image=[UIImage imageNamed:@"image.png"]; MyImage.userInteractionEnabled = YES; [self.view

iOS基础动画教程分享_IOS

iOS的动画多种多样,动画做的好的应用会更加吸引人,用起来也会更加炫目,本文介绍iOS几种基础动画,单个讲解便于理解,但真正使用时,结合起来用会看起来更加帅,这就看具体的应用场景和大家的想象力啦. 所有的基础动画都给予UIView一个基础的方法:animateWithDuration.这个方法可以包含一个代码块,里面设置要改变的东西,在执行的时候iOS会自动以动画的形式展现出来,代码如下: [UIView animateWithDuration:1 animations:^{ // 要执行的动作

iOS 利用动画和贝塞尔实现咻咻效果_IOS

先上效果图: 圆形 方形 思路分析: 这四种风格其实就是两种, 一种是动画效果在视图View的内部, 另一种是在视图的外部! 我们可以尝试封装自定义 View 设置相关属性去实现这两个风格. 点击时候触及动画, 说明要在这个 View 上添加手势! 分析动画效果其实是两种, 第一种是视图的比例由小到大,第二种是动画显示效果是渐渐变暗! 那么我们可以把两种效果写到一个动画组中!还有一个问题是效果的形状, 也就是 Layer 动画展示的形状有方形有圆形, 这个形状就需要我们思考如何去绘制和判断! 代

iOS 基本动画、关键帧动画、利用缓动函数实现物理动画效果_IOS

iOS基本动画/关键帧动画/利用缓动函数实现物理动画效果 先说下基本动画部分 基本动画部分比较简单, 但能实现的动画效果也很局限 使用方法大致为: #1. 创建原始UI或者画面 #2. 创建CABasicAnimation实例, 并设置keypart/duration/fromValue/toValue #3. 设置动画最终停留的位置 #4. 将配置好的动画添加到layer层中 举个例子, 比如实现一个圆形从上往下移动, 上代码: //设置原始画面 UIView *showView = [[UI

IOS绘制动画颜色渐变折线条_IOS

先给大家展示下效果图: 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有的也使用了动画,但是线条颜色渐变的折线图的demo少之又少,甚至可以说没有.该Blog阐述了动画绘制线条颜色渐变的折线图的实现方案,以及折线图线条颜色渐变的实现原理,并附以完整的示例. 成果 本人已将折线图封装到了一个UIView子类中,并提供了相应的接口.该自定义折线图视图,基本上可以适用于大部分需要集成折线图的项目.若你

iOS 视图---动画渲染机制探究

终端的开发,首当其冲的就是视图.动画的渲染,切换等等.用户使用 App 时最直接的体验就是这个界面好不好看,动画炫不炫,滑动流不流畅.UI就是 App 的门面,它的体验伴随着用户使用 App 的整个过程.如果UI失败,用户是不会有打开第二次的欲望的. iOS 为开发者提供了丰富的 Framework(UIKit,Core Animation,Core Graphic,OpenGL 等等)来满足开发从上层到底层各种各样的需求.不得不说苹果很牛逼,很多接口你根本不需要理解背后的原理就能上手使用并且满