A-生成Button,绑定事件和图片
- (UIButton*)buildButton:(CGRect)frame normalImgStr:(NSString*)normalImgStr highlightImgStr:(NSString*)highlightImgStr selectedImgStr:(NSString*)selectedImgStr action:(SEL)action parentView:(UIView*)parentView { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = frame; if (normalImgStr.length > 0) { [btn setImage:[UIImage imageNamed:normalImgStr] forState:UIControlStateNormal]; } if (highlightImgStr.length > 0) { [btn setImage:[UIImage imageNamed:highlightImgStr] forState:UIControlStateHighlighted]; } if (selectedImgStr.length > 0) { [btn setImage:[UIImage imageNamed:selectedImgStr] forState:UIControlStateSelected]; } [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; [parentView addSubview:btn]; return btn; }
UIButton * btn = [self buildButton:CGRectMake(fBtnX, 8, 30, 30) normalImgStr:[normalArr objectAtIndex:i] highlightImgStr:[highlightArr objectAtIndex:i] selectedImgStr:[selectedArr objectAtIndex:i] action:NSSelectorFromString([actionArr objectAtIndex:i]) parentView:parent]; btn.showsTouchWhenHighlighted = YES; [_cameraBtnSet addObject:btn];
//全部图片 NSMutableArray *normalArr = [[NSMutableArray alloc] initWithObjects:@"explore_cancel", @"camera_line_w.png", @"camera_change_w.png", @"camera_flash_w.png", nil]; //下边框 NSMutableArray *highlightArr = [[NSMutableArray alloc] initWithObjects:@"close_cha_h.png", @"", @"", @"", nil]; //选中后变色图片 NSMutableArray *selectedArr = [[NSMutableArray alloc] initWithObjects:@"", @"camera_line_h.png", @"camera_change_h.png", @"", nil]; //绑定事件 NSMutableArray *actionArr = [[NSMutableArray alloc] initWithObjects:@"dismissBtnPressed:", @"gridBtnPressed:", @"switchCameraBtnPressed:", @"flashBtnPressed:", nil];