IOS基础UI之(四)汤姆猫的实现
来源:互联网 发布:淘宝达摩盘 编辑:程序博客网 时间:2024/04/29 20:48
功能描述:
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覆盖上面,用于触发事件)。如下图:
//// 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
---------------文章至此!!
- IOS基础UI之(四)汤姆猫的实现
- ios-UI-汤姆猫德游戏实现
- **原创**零基础学ios**UI课程03***汤姆猫动画效果实现代码
- iOS开发UI基础—09UIImageView动画示例之汤姆猫程序
- iOS -UI汤姆猫实现(UIImageView)自己写的汤姆猫,所学知识有限,代码还有待改进 ==
- UI基础-基础控件-0324-汤姆猫(UIImageView的应用)
- iOS学习之路-简单汤姆猫
- iOS笔记UI--汤姆猫小项目(UIbutton使用)
- IOS--简单的汤姆猫代码
- iOS 自制汤姆猫
- 汤姆猫实现代码
- IOS汤姆猫小练习
- ios-day02-05(汤姆猫)
- 汤姆猫iOS案例总结
- iOS-汤姆猫项目总结
- IOS 汤姆猫核心代码
- swift之汤姆猫实现(UIImageView动画播放)
- 【成长记录】iOS小程序-不会说话的汤姆猫
- MyEclipse下Tomcat无法部署项目 finish按钮无法点击
- Spring事务管理
- ssl ssh
- 解析接口中的json数据
- PAT(B ) 1022. D进制的A+B
- IOS基础UI之(四)汤姆猫的实现
- 运算符重载
- MvvmLight(二)数据绑定DataBinding
- C++四大函数作业解析
- UIImageView以及动画
- [自用模板]持续更新!
- Android Studio创建module缺少build文件夹
- windows运行一个.exe的过程
- js将对象格式化成string