【问题标题】:Keep UIButton Selected/Highlighted after touch触摸后保持 UIButton 选中/突出显示
【发布时间】:2014-09-10 18:20:19
【问题描述】:

我希望我的按钮在用户点击后保持突出显示。如果用户再次点击该按钮,我希望它被取消选择/不突出显示。我不确定如何快速执行此操作。我目前正在使用界面生成器将按钮突出显示图像和选定图像设置为相同的 .png。

当我运行应用程序并点击按钮时,只要我的手指停留在按钮上,它就会变为我的突出显示图像。

【问题讨论】:

    标签: ios uibutton swift


    【解决方案1】:

    斯威夫特 5:

    @IBAction func toggleButton(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    

    【讨论】:

      【解决方案2】:

      这个对我来说很好用!

      func buttonColorChanger(sender :  UIButton )  {
      
          if button.isSelected == false
          {
      
              button.backgroundColor = UIColor.purple
              print("selected")
      
              button.setTitle("selected", for: .normal)
              button.setTitleColor(UIColor.white, for: .normal)
               button.isSelected = true
          }else{
      
              button.backgroundColor = UIColor.white
              print("unselected")
              button.isSelected = false
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用下面的代码 将isHighLighted 声明为实例变量

        //write this in your class
         var isHighLighted:Bool = false
        
        
        override func viewDidLoad() {
        
            let button  = UIButton(type: .system)
        
            button.setTitle("Your title", forState: UIControlState.Normal)
            button.frame = CGRectMake(0, 0, 100, 44)
        
            self.view.addSubview(button as UIView)
        
            button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        
        }
        
        func buttonClicked(sender:UIButton)
        {
            dispatch_async(dispatch_get_main_queue(), {
        
                if isHighLighted == false{
                    sender.highlighted = true;
                    isHighLighted = true
                }else{
                    sender.highlighted = false;
                    isHighLighted = false
                }
             });
        }
        

        我建议使用selected 状态而不是highlighted 以下代码演示所选状态

        override func viewDidLoad() {
        
            let button  = UIButton(type: .system)
        
            button.setTitle("Your title", forState: UIControlState.Normal)
            button.frame = CGRectMake(0, 0, 100, 44)
        
            self.view.addSubview(button as UIView)
            //set normal image 
            button.setImage(normalImage, forState: UIControlState.Normal)
            //set highlighted image 
            button.setImage(selectedImage, forState: UIControlState.Selected)
        
            button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        
        }
        
        func buttonClicked(sender:UIButton)
        {
              sender.selected = !sender.selected;
        }
        

        【讨论】:

        • 我没有看到您在代码中声明“isHighLigthed”的位置。我看到你在哪里检查“isHighLighted”的值,但不是你实际声明它的地方
        • class Abc{var isHighLighted:Bool = false 之后声明它。这将完美运行
        • @user3822654 你也使用选择状态,因为它更用于切换行为。你需要设置两个图像,一个用于normalState,另一个用于selectedState。希望这会有所帮助
        • 将按钮用作InterfaceBuilder没有问题,但你应该在类中删除isHighLighted。全局变量不好。如果在类中声明你有什么问题吗??
        • !sender.selected 不是可选的,它是not 运算符。它反转值。例如,如果aBool 具有true 而不是!aBool 返回false,并且如果您的按钮不是选择比sender.selected = !sender.selected; 这使发件人即。按钮selected
        【解决方案4】:
        func buttonPressed(_ sender: UIButton) {
        
            // "button" is a property
        
            if button.isSelected {
                button.setImage(UIImage(named: "filled-heart"), for: .normal)
                button.isSelected = false
            }else {
                button.setImage(UIImage(named: "empty-heart"), for: .selected)
                button.isSelected = true
            }
        }
        

        【讨论】:

          【解决方案5】:
          func highlightButton(button: UIButton) {
                 button.highlighted = true
          }
          
          @IBAction func touched(sender: UIButton) {
              let timer = NSTimer.scheduledTimerWithTimeInterval(0.0, target: self, selector: Selector("highlightButton(sender)"), userInfo: nil, repeats: true)
          }
          

          【讨论】:

          • 你能提供一个 Swift 的例子吗?
          • 哦抱歉不知道你想要它在 Swift 中
          • 现在,当我点击按钮时,应用程序崩溃,Xcode 突出显示我的 AppDelegate 中的第一行,并带有注释“线程 1:信号 SIGABRT”
          猜你喜欢
          • 1970-01-01
          • 2015-08-28
          • 2014-02-03
          • 2010-12-19
          • 2012-12-14
          • 2014-09-05
          • 2014-02-19
          • 1970-01-01
          • 2012-05-31
          相关资源
          最近更新 更多