UIButton的发现

15 篇文章 0 订阅

以前很少仔细研究UIButton的点击事件。
首先UIButton是UIControl的子类。
这次仅仅介绍方法- addTarget:action:forControlEvents:
下面是针对UIControlEventTouchUpInside的时间添加了两个方法,以前我认为只有第二个方法会执行。(现在看来这是错误的)

 [self.button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.button addTarget:self action:@selector(touchUpInside2:) forControlEvents:UIControlEventTouchUpInside];

这两个方法都会执行,按照添加的顺序执行即:touchUpInside:
->touchUpInside2:。再如:如果我再添加第一个方法,这样的执行顺序是touchUpInside2: ->touchUpInside:,同名的方法添加只会执行一次,执行顺序最后一次在哪添加他的执行顺序就在哪

 [self.button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.button addTarget:self action:@selector(touchUpInside2:) forControlEvents:UIControlEventTouchUpInside];
    [self.button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];

注:- addTarget:action:forControlEvents: 的官方解释:You may call this method multiple times, and you may specify multiple target-action pairs for a particular event. The action message may optionally include the sender and the event as parameters, in that order.

When you call this method, target is not retained.

titleEdgeInsets 和 imageEdgeInsets

imageEdgeInsets(top,left,bottom,right) 和 titleEdgeInsets(top,left,bottom,right)的值就是我们想要的最终结果与按钮初始状态比较图片和标题的位置分别在上下左右位移的值:
正常初始状态是 image 在左,title 在右

那么现在我们换一下,title 在左,image 在右,如图
这里写图片描述

1.首先计算 title 的width
2.图片上下位置不变top = 0, bottom = 0;向右位移一个title的宽度(实际图片是长宽比是13:7,假设长度是13,7 是宽度,图片在y坐标居中)
同时标题上下位置不变top = 0, bottom = 0;
向左位移一个图片的宽度和 5(title 与 image 的间隔),即 imageEdgeInsets = UIEdgeInsetsMake(0,0 + labelWidth + 5,0,0 - labelWidth -5);
图片相对初始状态左边距加了labelWidth+5,右边减(labelWidth+5),以及titleEdgeInsets = UIEdgeInsetsMake(0,0 - imageViewWidth,0, 0 + imageViewWidth);

CGSize size2 = [[_orderBtn titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _orderBtn.titleLabel.font}];

 _orderBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -13, 0, 13);
    _orderBtn.imageEdgeInsets = UIEdgeInsetsMake((40 - 7 )/2, size2.width+5 , (40 - 7)/2, -size2.width-5);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值