ios – 如何在按下UIButton状态后突出显示

前端之家收集整理的这篇文章主要介绍了ios – 如何在按下UIButton状态后突出显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个典型的要求,我需要按下按钮后突出显示状态.我需要执行一个任务,只有当一个按钮处于突出显示状态时才应该工作.其实我正在设置一个按钮状态,以编程方式突出显示.

[发件人设置高亮:YES];

一旦按钮处于突出显示状态,我需要执行另一个操作.

  1. - (IBAction)changeState: (UIButton*)sender
  2. {
  3. if (sender.highlighted == YES)
  4. {
  5. [self performSomeAtion:sender];
  6. }
  7. }

但是,对于我的恐惧,每当我按任何按钮,上述情况变得真实,并且重复执行动作.有没有什么办法可以保持UIButton的状态被压制后突出显示

编辑 – 实际上,我需要对3个不同的按钮状态执行3种不同的动作.我已经使用了选择的状态和正常状态.现在,我需要利用突出显示的状态.

解决方法

  1. [sender setSelected:YES];

或者您可以使用UIButton(notselectedimage.png和selectedimage.png)的两个图像来模拟此效果,然后使用BOOL buttonCurrentStatus等BOOL变量跟踪按钮状态.然后在.h文件

  1. BOOL buttonCurrentStatus;

和.m文件

  1. // connect this method with Touchupinside function
  2. - (IBAction)changeState:(UIButton*)sender
  3. {
  4. /* if we have multiple buttons,then we can
  5. differentiate them by tag value of button.*/
  6. // But note that you have to set the tag value before use this method.
  7.  
  8. if([sender tag] == yourButtontag){
  9.  
  10. if (buttonCurrentStatus == NO)
  11. {
  12. buttonCurrentStatus = YES;
  13. [butt setImage: [UIImage imageNamed:@"selectedImage.png"] forState:UIControlStateNormal];
  14. //[self performSomeAction:sender];
  15. }
  16. else
  17. {
  18. buttonCurrentStatus = NO;
  19. [butt setImage:[UIImage imageNamed:@"notSelectedImage.png"] forState:UIControlStateNormal];
  20. //[self performSomeAction:sender];
  21. }
  22. }
  23. }

猜你在找的iOS相关文章