IOS自定义UIButton九宫格效果

2025-05-29 0 41

此篇文章给大家描写如何写自定义九宫格,因为在开发中,这种需求也是常见不少。因此,主要利用UIButton阐述的;

实列不复杂,就两三个步骤完成:

一、定义宽度与高度(self.view)

?

1

2

3

4

5
#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))

二、定义九宫格的文字与图片

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
@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数据,以及相关样式动态调整

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35
-(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];

/*计算frame*/

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];

}

}

四、点击按钮的事件监听

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
-(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);

}];

}

总结:

如下逻辑分析:

1.上面用的是masonry布局,所以我的view容器就没用宽度,高度(写在我的view层里了)。
2.先定义一个View容器
3.在容器里,循环体里面定义button,设置button的属性等。
4.定义相关的数组,如:(文本,图片)
5.点击按钮事件触发函数;

就以上信息需要理解的逻辑,把上面的复制粘贴就可以,项目亲测可以的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 IOS自定义UIButton九宫格效果 https://www.kuaiidc.com/89130.html

相关文章

发表评论
暂无评论