【问题标题】:How do I switch between two colors on a UIView with UITapGestureRecognizer如何使用 UITapGestureRecognizer 在 UIView 上的两种颜色之间切换
【发布时间】:2015-04-19 12:54:46
【问题描述】:

我正在尝试设置一个手势识别器 (UITapGestureRecognizer),它通过单击一次并将视图颜色更改为蓝色并在第二次单击将其更改为黄色然后返回默认颜色红色来工作。

class ViewController: UIViewController {

    var squarView:UIView!;

    var squarIsBlue:Bool = true;

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        squarView = UIView(frame: CGRect(x: 130, y: 130, width: 150, height: 150));
        squarView.backgroundColor = UIColor.redColor();

        var squarTap = UITapGestureRecognizer(target: self, action: "tapMe:");

        squarView.addGestureRecognizer(squarTap);

        squarView.userInteractionEnabled = true;


        view.addSubview(squarView);
    }



    func tapMe(sender:UIGestureRecognizer){

        squarView.backgroundColor = UIColor.blueColor();
        if (squarIsBlue == false){
            squarView.backgroundColor = UIColor.yellowColor();
            if(squarIsBlue == true){
                squarView.backgroundColor = UIColor.redColor();

            }
            squarIsBlue = true

            }


            squarIsBlue = false;

            println("Squar Tapped!!!");

    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我做错了什么?

【问题讨论】:

    标签: ios swift view uiview uitapgesturerecognizer


    【解决方案1】:

    你的 tapMe 逻辑有误。在这种特殊情况下,最好使用整数变量而不是布尔值,因为您有 3 种状态(红色、蓝色、黄色)...

    试试这个:

        import UIKit
    
    class ViewController: UIViewController {
    
        private var tapGesture : UITapGestureRecognizer!
        private var childView : UIView!
    
        private let colorPalette = [UIColor.redColor(),UIColor.blueColor(),UIColor.yellowColor()]
        private var currentColorIdx = 0 {
            didSet{
                childView.backgroundColor = colorPalette[currentColorIdx]
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tapGesture = UITapGestureRecognizer(target: self, action: "didTap:")
    
            childView = UIView(frame: CGRect(x: 130, y: 130, width: 150, height: 150))
            childView.backgroundColor = colorPalette.first
            self.view.addSubview(childView)
        }
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            childView.addGestureRecognizer(self.tapGesture)
        }
    
        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(true)
            childView.removeGestureRecognizer(self.tapGesture)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        func didTap(gesture: UIGestureRecognizer){
            currentColorIdx = (currentColorIdx +  1 ) % colorPalette.count
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以将 squareIsBlue 变量设置为 int 而不是 Bool,以设置颜色,例如 0 = 红色(默认)、1 = 蓝色、2= 黄色、

      现在,在 viewdidLoad 中,设置类似

      squarIsBlue=0
      squarView.backgroundColor = UIColor.redColor();
      

      改变你的点击方法

      func tapMe(sender:UIGestureRecognizer){
      
          squarView.backgroundColor = UIColor.blueColor();
          if (squarIsBlue == 0){
              squarView.backgroundColor = UIColor.blueColor();
              squarIsBlue=1;
          else if (squarIsBlue == 1) {
              squarView.backgroundColor = UIColor.yellowColor();
              squarIsBlue =2
          }
          else if (squarIsBlue==2) {
              squarView.backgroundColor = UIColor.redColor();
              squarIsBlue=0;
          } 
          println("Squar Tapped!!!");
      }
      

      希望对你有帮助,

      HTH,享受编码!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 2013-07-06
        • 2012-09-12
        相关资源
        最近更新 更多