objective-c制作汤姆猫

来源:互联网 发布:knn算法c语言代码 编辑:程序博客网 时间:2024/04/28 12:51
#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView *imageView;@property (nonatomic, strong) UIButton *pieButton;@property (nonatomic, strong) UIButton *eatButton;@property (nonatomic, strong) UIButton *drinkButton;@property (nonatomic, strong) UIButton *fartButton;@property (nonatomic, strong) UIButton *cymbalButton;@property (nonatomic, strong) UIButton *scratchButton;@property (nonatomic, strong) UIButton *leftFootButton;@property (nonatomic, strong) UIButton *rightFootButton;@property (nonatomic, strong) UIButton *stoachButton;@property (nonatomic, strong) UIButton *headButton;@end@implementation ViewController#define kButtonH 60#define kButtonW 60- (void)viewDidLoad {    [super viewDidLoad];    //添加图像控件    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    [_imageView setImage:[UIImage imageNamed:@"angry_00.jpg"]];    [self.view addSubview:_imageView];        //添加pie按钮    _pieButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - kButtonH * 3, kButtonW, kButtonH)];    //[self.pieButton setBackgroundColor:[UIColor redColor]];    [self.pieButton setBackgroundImage:[UIImage imageNamed:@"pie"] forState:UIControlStateNormal];    [self.view addSubview:_pieButton];    [self.pieButton addTarget:self action:@selector(pie) forControlEvents:UIControlEventTouchUpInside];        //添加eat按钮    _eatButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - kButtonH * 2, kButtonW, kButtonH)];    //[self.eatButton setBackgroundColor:[UIColor blueColor]];    [self.eatButton setBackgroundImage:[UIImage imageNamed:@"eat"] forState:UIControlStateNormal];    [self.view addSubview:_eatButton];    [self.eatButton addTarget:self action:@selector(eat) forControlEvents:UIControlEventTouchUpInside];        //添加drink按钮    _drinkButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - kButtonH, kButtonW, kButtonH)];    //[self.drinkButton setBackgroundColor:[UIColor yellowColor]];    [self.drinkButton setBackgroundImage:[UIImage imageNamed:@"drink"] forState:UIControlStateNormal];    [self.view  addSubview:_drinkButton];    [self.drinkButton addTarget:self action:@selector(drink) forControlEvents:UIControlEventTouchUpInside];            //添加dfart按钮    _fartButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - kButtonW, self.view.frame.size.height - kButtonH * 3, kButtonW, kButtonH)];    //[self.fartButton setBackgroundColor:[UIColor yellowColor]];    [self.fartButton setBackgroundImage:[UIImage imageNamed:@"fart"] forState:UIControlStateNormal];    [self.view  addSubview:_fartButton];    [self.fartButton addTarget:self action:@selector(fart) forControlEvents:UIControlEventTouchUpInside];        //添加cymbal按钮    _cymbalButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - kButtonW, self.view.frame.size.height - kButtonH * 2, kButtonW, kButtonH)];    //[self.cymbalButton setBackgroundColor:[UIColor yellowColor]];    [self.cymbalButton setBackgroundImage:[UIImage imageNamed:@"cymbal"] forState:UIControlStateNormal];    [self.view  addSubview:_cymbalButton];    [self.cymbalButton addTarget:self action:@selector(cymbal) forControlEvents:UIControlEventTouchUpInside];        //添加scratch按钮    _scratchButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - kButtonW, self.view.frame.size.height - kButtonH, kButtonW, kButtonH)];    //[self.scratchButton setBackgroundColor:[UIColor yellowColor]];    [self.scratchButton setBackgroundImage:[UIImage imageNamed:@"scratch"] forState:UIControlStateNormal];    [self.view  addSubview:_scratchButton];    [self.scratchButton addTarget:self action:@selector(scratch) forControlEvents:UIControlEventTouchUpInside];        //添加rightFoot按钮    _leftFootButton = [[UIButton alloc] initWithFrame:CGRectMake(105, self.view.frame.size.height - 40, 47, 25)];    //[self.leftFootButton setBackgroundColor:[UIColor greenColor]];    [self.view  addSubview:_leftFootButton];    [self.leftFootButton addTarget:self action:@selector(footRight) forControlEvents:UIControlEventTouchUpInside];            //添加leftFoot按钮    _rightFootButton = [[UIButton alloc] initWithFrame:CGRectMake(165, self.view.frame.size.height - 40, 47, 25)];    //[self.rightFootButton setBackgroundColor:[UIColor yellowColor]];    [self.view  addSubview:_rightFootButton];    [self.rightFootButton addTarget:self action:@selector(footLeft) forControlEvents:UIControlEventTouchUpInside];        //添加head按钮    _headButton = [[UIButton alloc] initWithFrame:CGRectMake(90, 90, 140, 140)];    //[self.headButton setBackgroundColor:[UIColor yellowColor]];    [self.view  addSubview:_headButton];    [self.headButton addTarget:self action:@selector(knockOut) forControlEvents:UIControlEventTouchUpInside];        //添加stomach按钮    _stoachButton = [[UIButton alloc] initWithFrame:CGRectMake(100,  300, 120, 120)];    //[self.stoachButton setBackgroundColor:[UIColor blackColor]];    [self.view  addSubview:_stoachButton];    [self.stoachButton addTarget:self action:@selector(stomach) forControlEvents:UIControlEventTouchUpInside];}- (void)pie{    [self runAnimationWtihName:@"pie" AndImageNumbers:24];}- (void)eat{    [self runAnimationWtihName:@"eat" AndImageNumbers:40];}- (void)drink{    [self runAnimationWtihName:@"drink" AndImageNumbers:81];}- (void)fart{    [self runAnimationWtihName:@"fart" AndImageNumbers:28];}- (void)cymbal{    [self runAnimationWtihName:@"cymbal" AndImageNumbers:13];}- (void)scratch{    [self runAnimationWtihName:@"scratch" AndImageNumbers:56];}- (void)footLeft{    [self runAnimationWtihName:@"footLeft" AndImageNumbers:30];}- (void)footRight{    [self runAnimationWtihName:@"footRight" AndImageNumbers:30];}- (void)knockOut{    [self runAnimationWtihName:@"knockout" AndImageNumbers:81];}- (void)stomach{    [self runAnimationWtihName:@"stomach" AndImageNumbers:34];}- (void)runAnimationWtihName: (NSString *)name AndImageNumbers: (NSInteger) imageNumbers{    //如果有动画正在执行就直接返回,不执行新动画    if (self.imageView.isAnimating) {        return;    }    NSMutableArray *arrayM = [NSMutableArray array];    //往数组中添加照片    for (int i = 0;  i < imageNumbers; i++) {        //创建图片        //imageNamed:方法默认带有缓存,通过imageNamed:创建的图片会放到缓存中        //当你把图片放在Images.xcassets里面,就只能通过imageName加载        //当图片数量多的时候就会大量占据缓存,所以在图片较多时不适宜使用此种方法        //UIImage *image = [UIImage imageNamed:imageN];        //由于图片在Image.xcassets里,所以无法通过pathForResource:获取路径        NSString *imageN = [NSString stringWithFormat:@"%@_%02d", name, i];        NSString *path = [[NSBundle mainBundle] pathForResource:imageN ofType:@"jpg"];        UIImage *image = [UIImage imageWithContentsOfFile:path];        [arrayM addObject:image];    }    //把图片赋值给imageView动画数组(帧动画)    self.imageView.animationImages = arrayM;    //动画重复的次数    self.imageView.animationRepeatCount = 1;    //播放一次动画所需时间(默认情况是每张图片播放1/30秒)    self.imageView.animationDuration = 0.07 * imageNumbers;    //开始播放动画    [self.imageView startAnimating];    //清空animationImages的时间为动画执行完毕后0.1秒    CGFloat delay = self.imageView.animationDuration + 0.1;    //让self.imageView执行setAnimationImages:方法,来清空animationImages    [self.imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];}@end

0 0