#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