//_normalarray = @[@"like",@"More",@"Reply",@"share"];
_normalarray = [NSArray arrayWithObjects:@"脑炎后遗症",@"More",@"Reply",@"share", nil];
_selectedarray = @[@"矮小症",@"矮小症",@"矮小症",@"矮小症"];
for (int i = 0; i < 4; i++) {
_btntwo = [[UIButton alloc]initWithFrame:CGRectMake( i *(80 +2), 100, 120, 120)];
_btntwo.backgroundColor = [UIColor redColor];
[_btntwo addTarget:self action:@selector(clickbtntwo:) forControlEvents:UIControlEventTouchUpInside];
[_btntwo setBackgroundImage:[UIImage imageNamed:_normalarray[i]] forState:UIControlStateNormal];
_btntwo.tag = i+100;
_btntwo.selected = NO;
_btntwo.layer.cornerRadius = 60;
_btntwo.layer.masksToBounds = YES;
[_btntwo setBackgroundImage:[UIImage imageNamed:_selectedarray[i]] forState:UIControlStateSelected];
[self.view addSubview:_btntwo];
}
}
-(void)clickbtntwo:(UIButton *)sender{
for (int i = 0; i < _normalarray.count; i++) {
UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];
[btn setSelected:NO];
}
UIButton *button = (UIButton *)sender;
[button setSelected:YES];
}
本文介绍了一个iOS应用中的按钮组实现方案,包括按钮的布局、背景颜色设置及点击事件响应等。通过Objective-C语言实现了按钮的正常状态与选中状态之间的切换,并详细展示了如何设置按钮的图片资源。
1万+

被折叠的 条评论
为什么被折叠?



