如何使用 UITapGestureRecognizer 在 UIView 上的两种颜色之间切换

How do I switch between two colors on a UIView with UITapGestureRecognizer

我正在尝试设置一个手势识别器 (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.
    }


}

我做错了什么?

您可以将 squarIsBlue 变量设置为 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,享受编码吧!

你的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
    }

}