IOS基础UI之(四)汤姆猫的实现

    汤姆猫的实现核心就是一系列的图片动画播放效果。下面我们模拟汤姆猫,学习UIImageView实现动画效果。

  

 功能描述:

    1.相信很多人都玩过,汤姆猫就是根据用户的操作进行一系列的动作,具体不多说


 以下是其中几个效果图如下:



  UIImageView必须掌握的知识点。


 1.UIImageView创建和显示

 //创建

   UIImageView *imageView = [[UIImageView alloc]init];


  //设置图片。此方法有缓存(存入文件名)

  imageView.image =[UIImage imageNamed:@"tom.png"];  

  //没有缓存(传入全路径)

   NSBundle *bundle = [NSBundle mainBundle];

   NSString *path = [bundle pathForResource:@"tom.png" ofType:nil];

   UIImage *image = [UIImage imageWithContentsOfFile:path];


 2.highlightedImage 设置高亮状态下显示的图片

  imageView.highlightedImage = [UIImage imageNamed:@"tom.png"];

  [imageView setHighlighted:YES];


3. UIImageView 动画

  [imageView setAnimationImages:[NSArray array]]; //设置序列帧动画的图片数组

  [imageView setHighlightedAnimationImages:[NSArray array]] //设置高亮状态下序列帧动画的图片数组

  [imageView setAnimationDuration:0.3f]; //设置序列帧动画播放的时间

  [imageView setAnimationRepeatCount:2]; //设置序列帧动画播放的次数


4.设置是否允许用户交互,默认不允许用户交互

  [imageView setUserInteractionEnabled:YES];


  注意:在highlighted状态下设置的图片与序列帧动画要显示,必须同时设置UIImageView 的状态为highlighted


 实现过程:

1.倒入图片和plist


2.设计界面布局(头,肚子,尾巴...等 用UIButton覆盖上面,用于触发事件)。如下图:



3.代码如下:


//
//  ViewController.m
//  汤姆猫
//
//  Created by zxh on 15/8/24.
//  Copyright (c) 2015年 zxg. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tom;

- (IBAction)drink;
- (IBAction)cymbal;
- (IBAction)eat;
- (IBAction)fart;
- (IBAction)pie;
- (IBAction)scratch;

- (IBAction)knockout;
- (IBAction)stomach;
- (IBAction)footleft;
- (IBAction)footright;
- (IBAction)angry;

-(void)clearcache;

/** 图片动画*/
-(void) runAninationWithCount:(int)count andFileName:(NSString *)name;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 图片动画
-(void) runAninationWithCount:(int)count andFileName:(NSString *)name{
    //判断动画是否执行中
    if(self.tom.isAnimating) return;
    
    //加载图片到数组中
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i<count; i++) {
        NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];
        //此方法有缓存(存入文件名)
        //UIImage *image = [UIImage imageNamed:filename];
        
        //没有缓存(传入全路径)
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:filename ofType:nil];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [images addObject:image];
    }
    
    //设置图片
    self.tom.animationImages = images;
    
    //设置动画次数
    self.tom.animationRepeatCount = 1;
    
    //设置动画时间
    self.tom.animationDuration = count * 0.1;
    
    //开始动画
    [self.tom startAnimating];
    
    //动画播放完1秒后清除缓存
    CGFloat delay = self.tom.animationDuration +1;
    //[self performSelector:@selector(clearcache) withObject:nil afterDelay:delay];
    //等价于下面
    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}

//清理缓存
-(void)clearcache{
    NSLog(@"====clearcache====");
    self.tom.animationImages = nil;
    
    //[self.tom setAnimationImages:nil];
}

- (IBAction)drink {
    [self runAninationWithCount:81 andFileName:@"drink"];
    
}

- (IBAction)cymbal {
     [self runAninationWithCount:13 andFileName:@"cymbal"];
}

- (IBAction)eat {
     [self runAninationWithCount:40 andFileName:@"eat"];
}

- (IBAction)fart {
    [self runAninationWithCount:28 andFileName:@"fart"];
}

- (IBAction)pie {
    [self runAninationWithCount:24 andFileName:@"pie"];
}

- (IBAction)scratch {
    [self runAninationWithCount:56 andFileName:@"scratch"];
}

- (IBAction)knockout {
    [self runAninationWithCount:81 andFileName:@"knockout"];
}

- (IBAction)stomach {
    [self runAninationWithCount:34 andFileName:@"stomach"];
}

- (IBAction)footleft {
    [self runAninationWithCount:30 andFileName:@"footRight"];
}

- (IBAction)footright {
    [self runAninationWithCount:30 andFileName:@"footLeft"];
}

- (IBAction)angry {
    [self runAninationWithCount:26 andFileName:@"angry"];
}
@end


 ---------------文章至此!!奋斗



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
iOS开发中,可以使用多种框架来显示UI界面。其中最常用的是UIKit框架,它提供了创建和管理应用程序的用户界面的功能。\[3\]通过使用UIKit框架,开发者可以使用各种视图和控件来构建用户界面,例如按钮、标签、文本框等。此外,UIKit还提供了布局管理器来帮助开发者实现界面的自动布局和适配。\[1\] 除了UIKit框架,还有其他一些框架可以用于显示UI界面,例如QuartzCore框架可以提供动画特效和硬件渲染的能力,CoreGraphics框架提供了2D绘制的功能,CoreLocation框架可以使用GPS和WIFI获取位置信息,MapKit框架可以嵌入地图到应用程序中,AVFoundation框架可以进行音频处理。\[3\] 最近在WWDC 2019上,苹果发布了全新的SwiftUI框架,它可以帮助开发者更轻松地创建用户界面。SwiftUI使用声明式编程的方式,通过简洁的代码来描述界面的外观和行为。它支持Flex Box布局,可以使用PreviewProvider来提供预览数据,还支持简单的逻辑控制,如if语句。同时,SwiftUI与已有的Swift语法不冲突,可以与UIKit框架无缝集成。\[1\]\[2\] 因此,在iOS开发中,可以使用UIKit框架或者最新的SwiftUI框架来显示UI界面,具体选择取决于开发者的需求和偏好。 #### 引用[.reference_title] - *1* *2* [iOS Swift UI 绘制第一个UI界面](https://blog.csdn.net/WangQingLei0307/article/details/120664181)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [iOSUI界面和框架介绍](https://blog.csdn.net/yk_ddm/article/details/109190868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值