汤姆猫

#import "MainViewController.h"
#import "UIButton+MyButton.h"
@interface MainViewController ()

@property(nonatomic, retain)UIImageView *animationImageView;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)UIImageView *myView;

@end

@implementation MainViewController
- (void)dealloc{
    if (_animationImageView) {
        [_animationImageView release];
        [_picArr release];
    }
    [_myView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loadihFrame:[[UIScreen mainScreen] bounds]];
    
    _picArr = nil;
    self.myView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 110, 240, 400)];
    self.myView.image = [UIImage imageNamed:@"angry_00.jpg"];
    [self.view addSubview:self.myView];
    [self.myView release];
    
    NSMutableArray *picArray = [NSMutableArray array];
    
    NSArray *names = @[@"cymbal", @"drink", @"eat", @"fart", @"pie", @"scratch"];
    for (NSString *str in names) {
        NSString *nameStr = [NSString stringWithFormat:@"%@.png", str];
        UIImage *image = [UIImage imageNamed:nameStr];
        [picArray addObject:image];
        [image release];
    }
    
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:names];
    seg.frame = CGRectMake(100, 300, 450, 75);
    seg.transform = CGAffineTransformMakeRotation(90 * M_PI/180);
    [self.view addSubview:seg];
    for (NSInteger i = 0; i < names.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(290, 110 + 75 * i, 75, 75)];
        imageView.image = picArray[i];
        [self.view addSubview:imageView];
    }
    seg.tintColor = [UIColor clearColor];
    [seg release];
    [seg addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    
    CGRect footLeftFrame = CGRectMake(150, 470, 30, 30);
    CGRect footRightFrame = CGRectMake(120, 470, 30, 30);
    CGRect knockoutFrame = CGRectMake(110, 180, 80, 80);
    CGRect stomachFrame = CGRectMake(110, 380, 80, 80);

    NSArray *titles = @[@"footLeft", @"footRight", @"knockout", @"stomach"];
    CGRect buttonFrame[] = {footLeftFrame, footRightFrame, knockoutFrame, stomachFrame};
    
    for (NSInteger i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithFrame:buttonFrame[i] title:titles[i] target:self selector:@selector(click:)];
        [self.view addSubview:button];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)click:(id)seg{
    [self.animationImageView stopAnimating];
    self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 110, 240, 400)];
    [self.view addSubview:self.animationImageView];
    [self.view sendSubviewToBack:self.animationImageView];
    [self.view sendSubviewToBack:self.myView];
    [_animationImageView release];
    NSString *str = nil;
    if ([seg isKindOfClass:[UISegmentedControl class]]) {
       str = [NSString stringWithString:[seg titleForSegmentAtIndex:((UISegmentedControl *)seg).selectedSegmentIndex]];
    } else if([seg isKindOfClass:[UIButton class]]){
        str = ((UIButton *)seg).currentTitle;
    }
    NSInteger i = 0;
//    _picArr = [NSMutableArray array]; //尼玛 编译器傲娇了
    _picArr = [NSMutableArray array];
    while (1) {
        NSString *picName = [NSString  stringWithFormat:@"%@_%02ld.jpg",str, i];
        if ([UIImage imageNamed:picName] != nil) {
            UIImage *image = [UIImage imageNamed:picName];
            [self.picArr addObject:image];
            [image release];
            i++;
        } else break;
    }
    self.animationImageView.animationDuration = self.picArr.count * 0.1;
    self.animationImageView.animationImages = self.picArr;
    self.animationImageView.animationRepeatCount = 1;
    [self.animationImageView startAnimating];    
}
@end
#import "UIButton+MyButton.h"

@implementation UIButton (MyButton)
+ (id)buttonWithFrame:(CGRect)frame
                title:(NSString *)title
               target:(id)target
             selector:(SEL)selector{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    button.tintColor = [UIColor clearColor];
//    button.layer.borderWidth = 1;
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值