在工作中遇到的需求是 一个按钮要有两种状态,按住要发布语音,同时文字也要改变,经过尝试才知道一个Button 可以添加两种事件,小伙伴们遇到过吗?
//获取发布语音button self.pulishBtn = [pulishVocieView viewWithTag:103]; [self.pulishBtn addTarget:self action:@selector(pulishBtnTouchDown:) forControlEvents: UIControlEventTouchDown]; [self.pulishBtn addTarget:self action:@selector(pulishBtnTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; //获取提示label self.label = (UILabel *)[pulishVocieView viewWithTag:104];
还有一个需求是说 一个按钮要点击后,要由关注变成加关注,笔者的想法是利用两种状态,一个是默认,一个是被选中状态,结果去试了后,无论怎么点击,只有最后一个Cell的Button改变了,这下笔者可是蒙了,最后的解决的办法如下
UIButton *ActtonBtn = (UIButton *)[cell.contentView viewWithTag:104]; [ActtonBtn addTarget:self action:@selector(ActtionClick:) forControlEvents:UIControlEventTouchUpInside]; ActtonBtn.cornerRadius = 3; // self.auttenBtn = ActtonBtn; ActtonBtn.tag = self.count; self.count++;
在最上面有个属性,重改tag值就搞定了这个逻辑
@property (nonatomic, assign) int count;