ios UIImageView、UIButton

1.从Bundle内加载图片

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]];

2.用UIImageView做序列帧动画

    [zhaoImage setAnimationImages:imageList];
    [zhaoImage setAnimationDuration:1.0];
//    [zhaoImage setAnimationRepeatCount:1];
    [zhaoImage startAnimating];


3.UIButton设置标题文字、颜色

[button1 setTitle:@"别摸我" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

4.UIButton添加点击事件

[button1 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchDown];


实例:Tom猫

@property (nonatomic, strong) NSMutableArray *cymbalArray;//铜片
@property (nonatomic, strong) NSMutableArray *fartArray;//放屁
@property (nonatomic, strong) NSMutableArray *eatArray;//吃
@property (nonatomic, strong) NSMutableArray *drinkArray;//喝
@property (nonatomic, strong) NSMutableArray *scratchArray;//抓
@property (nonatomic, strong) NSMutableArray *pieArray;//扔球

@property (nonatomic, strong) NSMutableArray *blinkArray;//眨眼
@property (nonatomic, strong) NSMutableArray *yawnArray;//哈欠
@property (nonatomic, strong) NSMutableArray *sneezeArray;//打喷嚏
@property (nonatomic, strong) NSMutableArray *listenArray;//听
@property (nonatomic, strong) NSMutableArray *talkArray;//说

@property (nonatomic, strong) NSMutableArray *foot_leftArray;//左脚
@property (nonatomic, strong) NSMutableArray *foot_rightArray;//右脚
@property (nonatomic, strong) NSMutableArray *stomachArray;//胃
@property (nonatomic, strong) NSMutableArray *angryArray;//生气
@property (nonatomic, strong) NSMutableArray *happyArray;//开心

@property (nonatomic, strong) NSDictionary *dict;//全局字典
@property (nonatomic, strong) NSMutableDictionary *somenewDict;//局部字典
@property (nonatomic, strong) NSMutableArray *somenewMutableArray;//局部可变数组

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Tomcat" ofType:@"plist"];
    NSURL *url = [NSURL fileURLWithPath:path];
    self.dict = [NSDictionary dictionaryWithContentsOfURL:url];
    
    NSLog(@"%@", url);
    
    self.somenewDict = [NSMutableDictionary dictionary];
    self.somenewMutableArray = [NSMutableArray array];
}

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

- (IBAction)animationAction:(UIButton *)sender {
    if (![_bgImageView isAnimating]) {
        switch (sender.tag) {
            case KfartTag:
                [self setSomeAnimationWithAnimationName:@"fart"];
                NSLog(@"haha");
                break;
            case Kcymbal:
                [self setSomeAnimationWithAnimationName:@"cymbal"];
                break;
            case Kscratch:
                [self setSomeAnimationWithAnimationName:@"scratch"];
                break;
            case Keat:
                [self setSomeAnimationWithAnimationName:@"eat"];
                break;
            case Kdrink:
                [self setSomeAnimationWithAnimationName:@"drink"];
                break;
            case Kpie:
                [self setSomeAnimationWithAnimationName:@"pie"];
                break;
            case Ksneeze:
                [self setSomeAnimationWithAnimationName:@"sneeze"];
                break;
            case Khappy:
                [self setSomeAnimationWithAnimationName:@"happy"];
                break;
            case Kangry:
                [self setSomeAnimationWithAnimationName:@"angry-tail"];
                break;
            case Kfoot_left:
                [self setSomeAnimationWithAnimationName:@"foot_left"];
                break;
            case Kfoot_right:
                [self setSomeAnimationWithAnimationName:@"foot_right"];
                break;
            default:
                break;
        }

    }
}

- (void) setSomeAnimationWithAnimationName:(NSString *)animationName{
    NSLog(@"haha2");
    _somenewDict = _dict[animationName];
    
    for (NSInteger x = 0; x < [_somenewDict[@"frames"] integerValue]; x++) {
        NSString *imageFile = [NSString stringWithFormat:_somenewDict[@"imageFormat"], x];
        UIImage *image = [UIImage imageNamed:imageFile];
        [_somenewMutableArray addObject:image];
    }
    
    [_bgImageView setAnimationImages:_somenewMutableArray];
    [_bgImageView setAnimationRepeatCount:1];
    [_bgImageView setAnimationDuration:[_somenewDict[@"frames"] integerValue] / 10];
    [_bgImageView startAnimating];
    NSLog(@"haha3");
    
    [_somenewMutableArray removeAllObjects];
}
@end


你可能感兴趣的:(ios UIImageView、UIButton)