iOS 创建N个不规则按钮,宽度自适应
1.@property (nonatomic, copy) NSArray *irregularButtonTitleArray;
2. [self createIrregularButtons];
3.- (void)createIrregularButtons
{
UIView*bgView = [[UIViewalloc]init];
bgView.frame = CGRectMake(15, kApplicationStatusBarHeight, SCREEN_WIDTH-30, 100);
bgView.backgroundColor = [UIColor whiteColor];
bgView.tag=10000;
[self.viewaddSubview:bgView];
self.irregularButtonTitleArray = [[NSArray alloc] initWithObjects:@"我",@"是",@"一个",@"不规则的",@"按钮", nil];
// 这里要根据自己的需求来打开
// for (UIView *view in bgView.subviews) {
//
// if ([view isKindOfClass:[UIButton class]]) {
//
// UIButton *button = (UIButton *)view;
//
// if (button.tag >= 200) {
//
// [button removeFromSuperview];
// }
// }
// }
floatirregularButtonX =7.5;
floatirregularButtonY =15;
for(inti =0; i <self.irregularButtonTitleArray.count; i++){
//宽度自适应
NSDictionary *fontDict = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
CGRect frame_W = [self.irregularButtonTitleArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
// >SCREEN_WIDTH-30其中SCREEN_WIDTH-30就是bgView的宽度
// frame_W.size.width+30其中30可以改为任意其他值,相应的下面initWithFrame:也要加30
if(irregularButtonX+frame_W.size.width+30>SCREEN_WIDTH-30) {
irregularButtonX =7.5;
// irregularButton高度为30
// 竖直方向上的间隔为10
irregularButtonY +=40;
}
UIButton*irregularButton = [[UIButtonalloc]initWithFrame:CGRectMake(irregularButtonX, irregularButtonY, frame_W.size.width+30,30)];
[irregularButtonsetTitle:self.irregularButtonTitleArray[i] forState:UIControlStateNormal];
irregularButton.backgroundColor= [UIColorcolorWithRed:245/255.0green:245/255.0blue:245/255.0alpha:1.0];
irregularButton.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.16].CGColor;
irregularButton.layer.shadowOffset=CGSizeMake(0,1.5);
irregularButton.layer.shadowRadius=3;
irregularButton.layer.shadowOpacity=1;
irregularButton.layer.cornerRadius=7.5;
[irregularButtonsetTitleColor:RGB(144, 144, 153) forState:UIControlStateNormal];
[irregularButton.titleLabelsetFont:[UIFontsystemFontOfSize:14.0f]];
[irregularButtonsetTag:200+i];
[irregularButtonaddTarget:selfaction:@selector(irregularButtonClickedAction:)forControlEvents:UIControlEventTouchUpInside];
[bgViewaddSubview:irregularButton];
// 水平方向上的间隔为18
irregularButtonX =CGRectGetMaxX(irregularButton.frame)+18;
// +15其中15是irregularButtonY的初始值
bgView.frame=CGRectMake(15,kApplicationStatusBarHeight,SCREEN_WIDTH-30,CGRectGetMaxY(irregularButton.frame)+15);
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer*maskLayer = [[CAShapeLayeralloc]init];
maskLayer.frame= bgView.bounds;
maskLayer.path= maskPath.CGPath;
bgView.layer.mask= maskLayer;
}
- (void)irregularButtonClickedAction:(UIButton *)sender
{
NSString*currentIrregularButtonTitle =nil;
UIView*bgView = [self.viewviewWithTag:10000];
// 这里的遍历是为了找到点击的irregularButton,改变其backgroundColor和titleColor
for(UIView*viewinbgView.subviews) {
if([viewisKindOfClass:[UIButtonclass]]) {
UIButton*button = (UIButton*)view;
if(button.tag>=200) {
if(button.tag== sender.tag) {
[buttonsetBackgroundColor:mainColor];
[buttonsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}else{
[buttonsetBackgroundColor:RGB(245,245,245)];
[buttonsetTitleColor:RGB(144, 144, 153) forState:UIControlStateNormal];
}
}
}
}
currentIrregularButtonTitle =self.irregularButtonTitleArray[sender.tag-200];
NSLog(@"您当前点击的不规则按钮是:%@",currentIrregularButtonTitle);
}