Kof Kof - 20 days ago 5
iOS Question

Button control doesn't respond to taps in Swifty `Player` package view controller

Using

pod 'Player'
in an
iOS 9.0
app to play a video. I've subclassed
Player
class to add a UIButton overlay for closing the window.

It appears fine and has highlighting animation when tapped, but
closeTapped
isn't called when touching up inside.

import UIKit
import Player

class PlayerViewController: Player, PlayerDelegate {

func install() {
view.frame = presentor.view.bounds
presentor.addChildViewController(self)
presentor.view.addSubview(view)
didMove(toParentViewController: presentor)

let closeImage = UIImage(named: "close")!
let closeButton = UIButton(type: .custom)
view.addSubview(closeButton)
closeButton.setImage(closeImage, for: .normal)
closeButton.autoPinEdge(toSuperviewEdge: .top, withInset: 25)
closeButton.autoPinEdge(toSuperviewEdge: .right, withInset: 15)
closeButton.autoSetDimensions(to: CGSize(width: 50, height: 50))
closeButton.addGestureRecognizer(UITapGestureRecognizer())

closeButton.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
}

func closeTapped() {
logger.debug("Player close tapped")
}
}


I also tried having
closeTapped(sender: Any?)
, didn't help.

Why isn't
closeTapped
called?

Answer

You don't need to add a TapGestureRecognizer to the button. For swift 3.0 you can do it like this:

button.addTarget(self, action: #selector(closeTapped(sender:)), for: .touchUpInside)

func closeTapped(sender: UIButton) {
}
Comments