此篇文章给大家描写如何写自定义九宫格,因为在开发中,这种需求也是常见不少。因此,主要利用UIButton阐述的; 实列不复杂,就两三个步骤完成: 一、定义宽度与高度(self.vie
此篇文章给大家描写如何写自定义九宫格,因为在开发中,这种需求也是常见不少。因此,主要利用UIButton阐述的;
实列不复杂,就两三个步骤完成:
一、定义宽度与高度(self.view)
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define JHRGB(r,g,b) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]
#define JHRandomColor JHRGB(arc4random_unifORM(255), arc4random_uniform(255), arc4random_uniform(255))
二、定义九宫格的文字与图片
@property (nonatomic, strong) NSArray * titlesArr;
@property (nonatomic, strong) UILabel * numberLab;
@property (nonatomic, strong) NSArray * titleimg;
-(NSArray *)titlesArr{
if (!_titlesArr) {
_titlesArr = @[@"首页",@"采购",@"文章",@"社区",@"服务",@"扫描",@"定位",@"售后",@"订单"];
}
return _titlesArr;
}
-(NSArray *)titleimg{
if (!_titleimg) {
_titleimg = @[@"me",@"msg",@"meg",@"1",@"2",@"3",@"me",@"2",@"3"];
}
return _titleimg;
}
三、循环出9个UIBtton数据,以及相关样式动态调整
-(void)setButton{
NSInteger totalLoc = 3;//一列三个数
CGFloat W = 50;//宽度
CGFloat H = W;//高度
CGFloat margin=(self.view.frame.size.width-totalLoc * W)/(totalLoc+1);
for (NSInteger i = 0; i < self.titlesArr.count; i++) {//循环体
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];//button的定义
btn.frame = CGRectMake(100, 100, 80, 80);//button大小
[btn setTitle:self.titlesArr[i] forState:UIControlStateNormal];//动态设置button文本
[btn setBackgroundImage:[UIImage imageNamed:self.titleimg[i]] forState:UIControlStateNormal];//动态设置图片
[btn setTitleColor:[UIColor darkGrayColor] forState:0];//文本的颜色
[btn setImageEdgeInsets:UIEdgeInsetsMake(5, 25, 45, 25)];//图片的大小
[btn setTitleEdgeInsets:UIEdgeInsetsMake(80, 0, 5, 0)];//文本的位置
//btn.backgroundColor = [UIColor blueColor];
NSInteger row = i / totalLoc;//行号
NSInteger loc = i % totalLoc;//列号
//0/3=0,1/3=0,2/3=0,3/3=1;
//0%3=0,1%3=1,2%3=2,3%3=0;
CGFloat X= margin + (margin + W) * loc;
CGFloat Y= margin + (margin + H) * row;
btn.frame = CGRectMake(X, Y, W, H);
//设置tag值(这里的tag,只是为了让button的每次点击都有不同的动画效果)
btn.tag = i;
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.lgView.imgview addSubview:btn];
}
}
四、点击按钮的事件监听
-(void)clickBtn:(UIButton *)btn{
NSString *stringInt = [NSString stringWithFormat:@"%ld",(long)btn.tag];
btn.layer.transform = CATransform3DMakeScale(0.5*arc4random_uniform([stringInt floatValue]), 0.5*arc4random_uniform([stringInt floatValue]), 1);
self.numberLab.text = btn.titleLabel.text;
NSLog(@"%@wo dian ji l:",stringInt);
[UIView animateWithDuration:0.5 animations:^{
btn.layer.transform = CATransform3DMakeScale(1, 1, 1);
}];
}
总结:
如下逻辑分析:
上面用的是masonry布局,所以我的view容器就没用宽度,高度(写在我的view层里了)。 2.先定义一个View容器 3.在容器里,循环体里面定义button,设置button的属性等。 4.定义相关的数组,如:(文本,图片) 5.点击按钮事件触发函数;
就以上信息需要理解的逻辑,把上面的复制粘贴就可以,项目亲测可以的。
--结束END--
本文标题: IOS自定义UIButton九宫格效果
本文链接: https://www.lsjlt.com/news/30416.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0