1、UIImageView 动画

  • 1.1 播放图片集

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

  • 1.2 汤姆猫

    1. #import <AudioToolbox/AudioToolbox.h>
    2. @property (nonatomic, strong) UIImageView *playImageView;
    3. // 创建播放视图
    4. self.playImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    5. self.playImageView.image = [UIImage imageNamed:@"background.jpg"];
    6. [self.view addSubview:self.playImageView];
    7. // 创建功能按钮
    8. const CGFloat viewWith = self.view.bounds.size.width;
    9. const CGFloat viewHeight = self.view.bounds.size.height;
    10. const CGFloat gap = 10;
    11. const CGFloat buttonWith = self.view.bounds.size.width / 5;
    12. const CGFloat buttonHeight = buttonWith;
    13. // 功能按钮图片集
    14. NSArray *buttonImageNameArray = @[@"fart.png", @"cymbal.png", @"drink.png", @"eat.png", @"pie.png", @"scratch.png"];
    15. for (int i = 0; i < 11; i++) {
    16. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    17. [self.playImageView addSubview:button];
    18. self.playImageView.userInteractionEnabled = YES;
    19. if (i < 6) {
    20. // 两边功能按钮的布局
    21. if (i < 3) {
    22. button.frame = CGRectMake(gap, viewHeight / 2 + (buttonHeight + gap ) * (i % 3), buttonWith, buttonHeight);
    23. }
    24. else {
    25. button.frame = CGRectMake(viewWith - buttonWith - gap, viewHeight / 2 + (buttonHeight + gap) * (i % 3), buttonWith, buttonHeight);
    26. }
    27. [button setBackgroundImage:[UIImage imageNamed:buttonImageNameArray[i]] forState:UIControlStateNormal];
    28. } else {
    29. // 隐藏按钮的布局
    30. if (i == 6){ // 头
    31. button.frame = CGRectMake(viewWith/4, viewHeight/5, viewWith/2, viewHeight/4);
    32. }
    33. else if (i == 7){ // 肚子
    34. button.frame = CGRectMake(viewWith/3, viewHeight/3*2, viewWith/3, viewHeight/7);
    35. }
    36. else if (i == 8){ // 左脚
    37. button.frame = CGRectMake(viewWith/4*2, viewHeight/6*5, viewWith/6, viewHeight/7);
    38. }
    39. else if (i == 9){ // 右脚
    40. button.frame = CGRectMake(viewWith/4, viewHeight/6*5, viewWith/5, viewHeight/7);
    41. }
    42. else{ // 尾巴
    43. button.frame = CGRectMake(viewWith/9*6, viewHeight/7*5, viewWith/7, viewHeight/5);
    44. }
    45. // button.backgroundColor = [UIColor yellowColor];
    46. }
    47. button.tag = 100 + i;
    48. // 设置按钮事件
    49. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    50. }
    51. /********************** 点击按钮事件处理 ************************************/
    52. - (void)buttonClick:(UIButton *)button {
    53. switch (button.tag - 100) {
    54. case 0: // fart 放屁
    55. [self playAnimation:@"fart"];
    56. [self performSelector:@selector(playVoice:) withObject:@"fart" afterDelay:0.5];
    57. break;
    58. case 1: // cymbal 敲锣
    59. [self playAnimation:@"cymbal"];
    60. [self performSelector:@selector(playVoice:) withObject:@"cymbal" afterDelay:0.5];
    61. break;
    62. case 2: // drink 喝牛奶
    63. [self playAnimation:@"drink"];
    64. [self performSelector:@selector(playVoice:) withObject:@"drink" afterDelay:0.5];
    65. break;
    66. case 3: // eat 吃小鸟
    67. [self playAnimation:@"eat"];
    68. [self performSelector:@selector(playVoice:) withObject:@"eat" afterDelay:0.5];
    69. break;
    70. case 4: // pie 撇东西
    71. [self playAnimation:@"pie"];
    72. [self performSelector:@selector(playVoice:) withObject:@"pie" afterDelay:0.5];
    73. break;
    74. case 5: // scratch 抓屏幕
    75. [self playAnimation:@"scratch"];
    76. [self performSelector:@selector(playVoice:) withObject:@"scratch" afterDelay:1.5];
    77. break;
    78. case 6: // knockout 头
    79. [self playAnimation:@"knockout"];
    80. [self performSelector:@selector(playVoice:) withObject:@"knockout" afterDelay:0.5];
    81. break;
    82. case 7: // stomach 肚子
    83. [self playAnimation:@"stomach"];
    84. [self performSelector:@selector(playVoice:) withObject:@"stomach" afterDelay:0.5];
    85. break;
    86. case 8: // foot_left 左脚
    87. [self playAnimation:@"foot_left"];
    88. [self performSelector:@selector(playVoice:) withObject:@"foot_left" afterDelay:0.5];
    89. break;
    90. case 9: // foot_right 右脚
    91. [self playAnimation:@"foot_right"];
    92. [self performSelector:@selector(playVoice:) withObject:@"foot_right" afterDelay:0.5];
    93. break;
    94. case 10: // angry 尾巴
    95. [self playAnimation:@"angry"];
    96. [self performSelector:@selector(playVoice:) withObject:@"angry" afterDelay:0.8];
    97. break;
    98. default:
    99. break;
    100. }
    101. }
    102. /********************** 播放动画 ************************************/
    103. - (void)playAnimation:(NSString *)key {
    104. // 读取 plist 文件获取图片数量
    105. NSDictionary *imageNumDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TomCat" ofType:@"plist"]];
    106. int imageNum = [[imageNumDictionary objectForKey:key] intValue];
    107. NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:0];
    108. for (int i = 0; i < imageNum; i++) {
    109. [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%.2d.jpg", key, i]]];
    110. }
    111. self.playImageView.animationImages = imageArray;
    112. self.playImageView.animationDuration = imageNum/13;
    113. self.playImageView.animationRepeatCount = 1;
    114. [self.playImageView startAnimating]; // 播放动画
    115. }
    116. /********************** 播放声音 ************************************/
    117. - (void)playVoice:(NSString *)key {
    118. // 添加声音
    119. SystemSoundID soundID;
    120. AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:key ofType:@"wav"]]), &soundID);
    121. AudioServicesPlayAlertSound(soundID); // 播放声音
    122. }
    • 效果

      ------

UIImageView 动画的更多相关文章

  1. iOS - UIImageView 动画

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

  2. UIImageView 动画 / UIImage 方向

    UIImage 方向 UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向 如果本身是横屏, 照片也是横屏的话, 方向是正方向 BOOL b1 = (originalIma ...

  3. UIImageView动画制作

    1.先初始化一个UIImageView的视图窗口 如:anima UIImageView *anima = [UIImageView alloc]initWithFrame(0,0,100,100); ...

  4. 通过cagradientLayer类封装uiimageview动画色度差

    #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, EcolorDirectionType) { EcolorDirectionUp, / ...

  5. UIImageView~动画播放的内存优化

    我目前学到的知识,播放动画的步骤就是下面的几个步骤,把照片资源放到数组里面,通过动画animationImage加载数组,设置动画播放的 时间和次数完成播放. 后来通过看一些视频了解到:当需要播放多个 ...

  6. UIImageView动画

    NSMutableArray *arrM = [NSMutableArray array]; // 2.加载所有图片 ; i <= ; i++) { NSString *imageName = ...

  7. iOS - Core Animation 核心动画

    1.UIView 动画 具体讲解见 iOS - UIView 动画 2.UIImageView 动画 具体讲解见 iOS - UIImageView 动画 3.CADisplayLink 定时器 具体 ...

  8. 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )

    转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...

  9. iOS常用技术

    1.判断系统 #define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersi ...

随机推荐

  1. springboot成神之——log4j2的使用

    本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...

  2. Python数据分析 EPD

    参考用书 <利用Python进行技术分析:Python for Data Analysis> 官方把epd (https://www.enthought.com/products/cano ...

  3. Super Jumping! Jumping! Jumping(最大递增子序列的和)

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...

  4. vim 添加插件

    vim 的功能可以通过向它添加plugin得以扩展.所谓的plugin不过是一个vim会自动载入执行的脚本.把一个脚本放到你的plugin目录就可以了,非常容易. plugin基本上分为两类:  全局 ...

  5. Win10 Tensorflow 配置Mask_RCNN

    1.安装Anaconda3 下载地址  Anaconda 官网下载地址:https://www.continuum.io/downloads 下载以后,点击exe程序,开始安装,详细的安装过程(图片参 ...

  6. openebula vm无法获取IP问题解决

    http://archives.opennebula.org/documentation:archives:rel2.2:cong Contextualizing Virtual Machines 2 ...

  7. 8-python模拟登入(无验证码)

    方式: 1.手动登入,获取cookie 2.使用cookielib库 和 HTTPCookieProcessor处理器 #_*_ coding: utf-8 _*_ ''' Created on 20 ...

  8. lucene 第二天

    Lucene/Solr   第二天 1. 课程计划 Lucene的Field Lucene的索引库维护 lucene的查询 a) Query子对象 b) QueryParser Lucene相关度排序 ...

  9. 当集合里存储的是URL时的一些问题总结

    先看道题吧: package com.lk.C; import java.net.MalformedURLException; import java.net.URL; import java.uti ...

  10. c语言交换两个变量的值

    有两个变量a 和b,想要交换它们的值 int a,b; 能不能这样操作呢? b=a; a=b; 不能啊,这样操作的意思是把a的值放到b中,然后b中的值已经被覆盖掉了,已经不是b原来的那个值了,所以是没 ...