iOS 自制汤姆猫
2015-10-06 16:00
357 查看
效果图如下:
这个汤姆猫主要是利用button来控制图片而已,不过我们的图片是所有的图片和在一起的,看起来就像是一连串的动作。本人知识有限,如有其它建议或者意见,必会虚心 改进。
#import "twoViewController.h" @interface twoViewController () { UIImageView *imageView; UIImageView *animationView_stomach; NSMutableArray *imageList_stomach; UIImageView *animationView_footleft; NSMutableArray *imageList_footleft; UIImageView *animationView_footright; NSMutableArray *imageList_footright; } @end @implementation twoViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; imageView.image = [UIImage imageNamed:@"fart_00.jpg"]; [self.view addSubview:imageView]; // 胃部 UIButton *b_stomach = [UIButton buttonWithType:UIButtonTypeCustom]; b_stomach.frame = CGRectMake(150, 450, 80, 100); [b_stomach addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside]; b_stomach.tag = 100; [self.view addSubview:b_stomach]; // 左脚 UIButton *b_footleft = [UIButton buttonWithType:UIButtonTypeCustom]; b_footleft.frame = CGRectMake(190, 600, 50, 50); [b_footleft addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside]; b_footleft.tag = 101; [self.view addSubview:b_footleft]; // 右脚 UIButton *b_footright = [UIButton buttonWithType:UIButtonTypeCustom]; b_footright.frame = CGRectMake(130, 600, 50, 50); [b_footright addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside]; b_footright.tag = 102; [self.view addSubview:b_footright]; NSArray *arr = @[@"scratch",@"pie",@"drink",@"fart",@"eat",@"cymbal"]; // 左边三个按钮动作 for (int i = 0; i<3; i++) { UIButton *bleft = [UIButton buttonWithType:UIButtonTypeCustom]; bleft.frame = CGRectMake(0, 300+(50+10)*i, 50, 50); bleft.tag = i+1; [bleft setBackgroundImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal]; [self.view addSubview:bleft]; [bleft addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside]; } // 右边三个按钮动作 for (int j = 0; j<3; j++) { UIButton *bright = [UIButton buttonWithType:UIButtonTypeCustom]; bright.frame = CGRectMake(375-50, 300+(50+10)*j, 50, 50); bright.tag = j+10; [bright setBackgroundImage:[UIImage imageNamed:arr[3+j]] forState:UIControlStateNormal]; [self.view addSubview:bright]; [bright addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside]; } } - (void)loadAnimationWithCount:(int)count name:(NSString *)name { if ([imageView isAnimating]) { return; } // 把图片里面的数据 加载到内存 然后再从内存里面 获得图片数据 NSMutableArray *images = [NSMutableArray array]; // 如果读取高质量的图片 可以采取以下方式 for (int i =0; i<count; i++) { NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",name,i]; NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil]; UIImage *image = [UIImage imageWithContentsOfFile:path]; [images addObject:image]; } imageView.animationImages = images; imageView.animationRepeatCount = 1; imageView.animationDuration = images.count*0.05; [imageView startAnimating]; CGFloat delay = imageView.animationDuration +0.1; [imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay]; } - (void)leftAction:(UIButton *)sender { switch (sender.tag) { case 1: [self loadAnimationWithCount:55 name:@"scratch"]; break; case 2: [self loadAnimationWithCount:23 name:@"pie"]; break; case 3: [self loadAnimationWithCount:80 name:@"drink"]; break; case 100: [self loadAnimationWithCount:33 name:@"stomach"]; break; case 101: [self loadAnimationWithCount:29 name:@"foot_left"]; break; case 102: [self loadAnimationWithCount:29 name:@"foot_right"]; break; default: break; } } - (void)rightAction:(UIButton *)sender { switch (sender.tag) { case 10: [self loadAnimationWithCount:27 name:@"fart"]; break; case 11: [self loadAnimationWithCount:39 name:@"eat"]; break; case 12: [self loadAnimationWithCount:12 name:@"cymbal"]; break; default: break; } }
这个汤姆猫主要是利用button来控制图片而已,不过我们的图片是所有的图片和在一起的,看起来就像是一连串的动作。本人知识有限,如有其它建议或者意见,必会虚心 改进。
相关文章推荐
- iOS软件开发 如何做一个选择器
- 关于iOS9适配中出现的问题
- iOS中的动画总体预览
- iOS数据的持久化(沙盒)
- Xcode 7.0过后,也就是iOS9.0过后适配时出现的问题
- HTML5-IOS WEB APP应用程序(IOS META)
- iOS开发如何让透明View里的对象不透明
- [iOS进阶]自己动手写KVO总结
- 你所知道好玩有趣的 iOS URL schemes 有哪些?
- iOS去掉字符串中的空格,换行符号
- 近期苹果公司iOS 9系统策略更新
- iOS开发----三目运算符
- iOS:多线程的详细介绍
- IOS中级篇——何时使用copy, strong, weak, assign关键字定义属性
- iOS获取系统时间
- iOS静态库小结--(yoowei)
- 【ios编程报错处理-1】错误提示:App transport security has blocked a exceptions can be configured via your app's
- iOS经典讲解之地图定位请求位置信息时出现的问题
- iOS --- 多语言适配的注意事项
- iOS 使用百度图表插件Echarts