如何用UITapGestureRecognizer在UIView上切换两种颜色

时间:2021-02-21 19:36:20

I am trying to set up a gesture recognizer (UITapGestureRecognizer) that works by clicking once and changing the view color to blue and in the second click to change it to yellow and then back to the default color red.

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


}

What am I doing wrong?

我做错了什么?

2 个解决方案

#1


0  

You can set your squarIsBlue variable as int instead of Bool, to set color like, 0 = Red (Default), 1 = Blue, 2= Yellow,

可以将squarIsBlue变量设置为int而不是Bool,设置颜色,比如,0 = Red(默认),1 = Blue, 2=黄色,

So now, In viewdidLoad, set something like

现在,在viewdidLoad中,设置一些类似的东西。

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

Change your Tap Method like

改变你的Tap方法

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!!!");
}

Hope, this help you,

希望,这帮助你,

HTH, Enjoy coding !!

HTH,享受编码!

#2


2  

You have mistake in tapMe logic. In this particular case it's better to use Integer variable instead of Boolean, since you have 3 states (Red,Blue,Yellow)...

你在逻辑上犯了错误。在这种情况下,最好使用整型变量而不是布尔值,因为您有三种状态(红、蓝、黄)……

Try this :

试试这个:

    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
    }

}

#1


0  

You can set your squarIsBlue variable as int instead of Bool, to set color like, 0 = Red (Default), 1 = Blue, 2= Yellow,

可以将squarIsBlue变量设置为int而不是Bool,设置颜色,比如,0 = Red(默认),1 = Blue, 2=黄色,

So now, In viewdidLoad, set something like

现在,在viewdidLoad中,设置一些类似的东西。

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

Change your Tap Method like

改变你的Tap方法

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!!!");
}

Hope, this help you,

希望,这帮助你,

HTH, Enjoy coding !!

HTH,享受编码!

#2


2  

You have mistake in tapMe logic. In this particular case it's better to use Integer variable instead of Boolean, since you have 3 states (Red,Blue,Yellow)...

你在逻辑上犯了错误。在这种情况下,最好使用整型变量而不是布尔值,因为您有三种状态(红、蓝、黄)……

Try this :

试试这个:

    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
    }

}