简体   繁体   中英

How to add gesture recognizer to custom UIView (XIB) programmatically?

Intro

I trying to adding a tap recognizer to an instance of my custom UIView class CardView . So I created a custom view xib and link my Swift class file CardView.swift via File's Owner . After that I connected each outlet to the corresponding UI elements.

I would like to add my custom View CardView programmatically to my ViewController and attach a tapGesture to the newly created view.

CardView class:

class CardView: UIView {

    // Delcare outlets
    @IBOutlet var view: UIView!
    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var icon: UIImageView!


    //MARK: Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)

        Bundle.main.loadNibNamed("CardView", owner: self, options: nil)
        addSubview(view)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }

}

ViewController class

In the viewDidLoad() function of ViewController.swift I created an instance of CardView():

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

        let cardView = CardView()
        self.view.addSubview(cardView)

        // Style cardView instance
        cardView.view.backgroundColor = UIColor.yellow
        cardView.view.center.x = self.view.center.x
        cardView.view.center.y = self.view.center.y

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))
        cardView.addGestureRecognizer(tapGesture)

    }

Than I add tapGesture to my cardView just like normal using Swift:

let tapGesture = UIPanGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))    cardView.addGestureRecognizer(panGesture)

And add the corresponding function to be called:

func handleTap(gestureRecognizer: UIGestureRecognizer) {
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

Main question/problem

The problem that I have with adding a tap Gesture Recognizer to the newly created CardView - an instance of the CardView - is that the function handleTap() is never been called.

I already tried several things like add cardView.isUserInteractionEnabled = true to both files ( ViewController.swift and CardView.swift class). Also add in one of these files and not in the other one.

After declaring the type of gesture, I also try to add it my the super view, the one that is linked with the ViewController.swift file . When I tap on the screen, the function will be triggered, but not with my own created custom UIView.

Beside those I also tried to add UIGestureRecognizerDelegate to my VC class with no result.

class ViewController: UIViewController, UIGestureRecognizerDelegate {}

In my viewDidLoad() function before I connect the gesture to my cardView I added this extra line: tapGesture.delegate = self .

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))
tapGesture.delegate = self
architectView.addGestureRecognizer(tapGesture)

I had same issue with Custom UIView.

I made Xib and swift file and connected it. Then add gesture to UIView, however, it doesn't work. I changed UIView to UIButton to solve the problem but doesn't work.

To solve my problem is

AutoLayout Problem

When I run the app and debug by Debug view hierarchy , there is ambiguous problem with my customUIView.

By print description of UIView, I can notify it has frame(0,0), subView has own frame on the other hand.

So I delete and make from the scratch, run test each time and solve it. Also gesture function works when clicked custom UIView.

I hope this works to you. I saw your code and it doesn't seem have problem from your code, so check xib file and auto layout those label and image.

更正UIPanGestureRecognizer(target: self, action: #selector(dragged))

UIPanGestureRecognizer(target: self, action: #selector(dragged(sender:)))

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