tom猫的UI

来源:互联网 发布:条码数据采集 编辑:程序博客网 时间:2024/05/06 14:13

   今天我学习了ios中的UIButton属性和UIImageView属性的相关用法,做了一个简易的tom猫UI程序。

1. 搭建UI界面

2.实现按钮监听

3.代码重构

3.性能优化










////  GJJViewController.h//  tom猫////  Created by apple on 14-4-25.//  Copyright (c) 2014年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface GJJViewController : UIViewController@property (weak, nonatomic) IBOutlet UIImageView *tomCat;// tom猫的行为- (IBAction)tomCatBehaviour:(UIButton *)sender;@end


////  GJJViewController.m//  tom猫////  Created by apple on 14-4-25.//  Copyright (c) 2014年 apple. All rights reserved.//#import "GJJViewController.h"#define kPlayerTime 0.1#define kPlayerCount 1#define kDocumentName @"tom"#define kDocumentLastName @"plist"@interface GJJViewController (){    // 将tom猫程序的播放图片名称和数量抽取到tom.plist文件中    NSDictionary *_tomDict;}@end@implementation GJJViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    // 获得tom.plist文件的全路径    NSBundle *bundle = [NSBundle mainBundle];    // 获得全路径    NSString *name = [bundle pathForResource:kDocumentName ofType:kDocumentLastName];    // 获得文件中的字典    _tomDict = [NSDictionary dictionaryWithContentsOfFile:name];    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark tom猫动画- (void)tomCatBehaviourAnimating:(NSString *)name withImageCount:(int)count {        // 通过bundle拿到图片的全路径    NSBundle *bundle = [NSBundle mainBundle];        // 定义可变数组来存储图片    NSMutableArray *tomAnimationImages = [[NSMutableArray alloc] init];    for (int i = 0; i < count; i ++) {                // 图片名称        NSString *tomImageName = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];                // 获得全路径        NSString *tomImageNamePath = [bundle pathForResource:tomImageName ofType:nil];                // 通过图片全路径来获得图片        UIImage *tomImage = [[UIImage alloc] initWithContentsOfFile:tomImageNamePath];                // 添加图片        [tomAnimationImages addObject:tomImage];    }        // 设置播放动画的图片    _tomCat.animationImages = tomAnimationImages;        // 设置播放动画的时间    _tomCat.animationDuration = kPlayerTime * count;        // 设置播放动画的次数    _tomCat.animationRepeatCount = kPlayerCount;        // 开启动画    [_tomCat startAnimating];}#pragma mark tom猫的行为- (IBAction)tomCatBehaviour:(UIButton *)sender {        if ( [_tomCat isAnimating] ) return;        // 获得普通状态下按钮的名称    NSString *name = [sender titleForState:UIControlStateNormal];        // 获得图片播放的张数    int count = [_tomDict[name] intValue];        // 播放动画    [self tomCatBehaviourAnimating:name withImageCount:count];    }@end





0 0
原创粉丝点击