ZED
ZED

Reputation: 1

how to detect when user click lyft Button swift

We want Lyft button touch event because I am working in analytics, so, I need how many people choose Lyft but I can't put UIView click event. I try below code.

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
cell.lyftButton.addGestureRecognizer(gesture)

How can i achieve this?

Upvotes: 0

Views: 232

Answers (2)

To retrieve the LyftButton, you'll need to fetch the button inside the Lyft view, after retrieving it, I tried to add another target to it which was your 'checkAction' method, but for some reason it is not being called. One workaround solution is:

  • On Auto Layout, created a transparent button on top of the Lyft Button View, let's callet it 'Transparent Lyft Button': Example (I've embeded in another view because it was on a stackView);
  • On the code, retrieved the button with the above method, held it in a variable, let's call it 'requestLyftButton' and disabled it.
  • Created an IBAction for the 'Transparent Lyft Button' that triggers the method 'self.checkAction' that you've created and also calls requestLyftButton.sendActions(for: .touchUpInside), which triggers the original Lyft SDK action.

To Retrieve Lyft UIButton:

@IBOutlet weak var lyftButton: LyftButton!
@IBOutlet weak var transparentLyftButton: UIButton!
var requestLyftButton: UIButton?

func retrieveLyftButton(in view: UIView) {
    for view in view.subviews {
        if let lyftBtn = view as? UIButton {
            lyftBtn.isEnabled = false
            requestLyftButton = lyftBtn
        } else {
            retrieveLyftBtn(in: view)
        }
    }
}

transparentLyftButton IBAction to trigger your method + lyft sdk original action:

@IBAction func requestLyft(_ sender: UIButton) {
    if let lyftBtn = requestLyftButton {
        checkAction() // Your method
        lyftBtn.sendActions(for: .touchUpInside)
    }
}

I hope that you can understand what was done, if you have any questions, just let me know.

Upvotes: 0

Najeeb ur Rehman
Najeeb ur Rehman

Reputation: 413

You can directly assign a selector method to lyftButton e.g

lyftButton.addTarget(self, action: #selector(lyftButtonAction(_:)), for: .touchUpInside)

@objc
func lyftButtonAction(_sender: UIButton) {
//Do your action
}

Upvotes: 0

Related Questions