简体   繁体   中英

Tapping a view in xcode / swift 3 to 'select' it

I'm looking to hone in on the methodology used to 'select' UIViews by a single tap. In my UI, I have several UIViews that I would like to be 'selectable.' While selected, I will have controls to perform basic adjustments such as alpha (to let the user know which view is selected), and slightly more advanced functionality such as revealing a unique set of menu items. Only 1 view may be selected at a time. No multi-touch here.

The problem is, I've only seen this done with UICollectionViewCells. My interface isn't utilizing a collection view, so that's out of the question.

Thought process going into this is 1 tap gesture to 'select' the uiview, and then 'deselecting' it by tapping outside of the views bounds.

Another thought process is going with UIButtons instead of UIViews to utilize the Selected state.

Which would be the more viable solution here? Is this even possible using just UIViews? Or should I backtrack and go UIButtons instead?

Thanks.

You need to add a gesture recognizer to the desired UIView

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    @IBOutlet weak var myView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:)))
        tap.delegate = self
        myView.addGestureRecognizer(tap)

    }

}

Then you need to add the Code to handle whatever happens to your UIView into the gesture recognizer function:

func tap(_ gestureRecognizer: UITapGestureRecognizer) {
    myView.alpha = 0.5
}

This will change UIView's alpha property to 0.5 once the UIView gets selected. 在此处输入图片说明 在此处输入图片说明

Now you could for example put a second UIView with a gesture recognizer and play with the alphas:

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    @IBOutlet weak var myView: UIView!
    @IBOutlet weak var secondView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:)))
        tap.delegate = self
        myView.addGestureRecognizer(tap)

        let tap2 = UITapGestureRecognizer(target: self, action: #selector(self.tap2(_:)))
        tap.delegate = self
        secondView.addGestureRecognizer(tap2)

    }

    func tap(_ gestureRecognizer: UITapGestureRecognizer) {
        myView.alpha = 0.5
        secondView.alpha = 1
    }

    func tap2(_ gestureRecognizer: UITapGestureRecognizer) {
        myView.alpha = 1
        secondView.alpha = 0.5
    }

}

That way, depending on the UIView you hit, the alpha will change...

在此处输入图片说明 在此处输入图片说明

Edit:

In this Code the UIViews will be "selected" - the alpha will change and will change back within a set timing of 0.8 seconds.

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    @IBOutlet weak var myView: UIView!
    @IBOutlet weak var secondView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:)))
        tap.delegate = self
        myView.addGestureRecognizer(tap)

        let tap2 = UITapGestureRecognizer(target: self, action: #selector(self.tap2(_:)))
        tap.delegate = self
        secondView.addGestureRecognizer(tap2)

    }

    func tap(_ gestureRecognizer: UITapGestureRecognizer) {
        myView.alpha = 0.5
        UIView.animate(withDuration: 0.8, animations: { 
            self.myView.alpha = 1
        })
    }

    func tap2(_ gestureRecognizer: UITapGestureRecognizer) {
        secondView.alpha = 0.5
        UIView.animate(withDuration: 0.8, animations: {
            self.secondView.alpha = 1
        })
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM