UILabel tap gesture recognizer doesn't seem to work - ios

I know this is an often asked question but I have a specific case of the issue as I abstracted the tapGestureRecognizer so it can be re-used without initialiazing it everytime.
Here is how I use UITapGestureRecognizer on UIlabels :
In UILabel extension :
func setOnClickListener(action: #escaping (_: Any?) -> Void, clickedObject: Any? = nil){
let tapRecogniser = ClickListener(target: self, action: #selector(onViewClicked), clickedObject: clickedObject, onClick: action)
self.addGestureRecognizer(tapRecogniser)
}
#objc func onViewClicked(_ sender: ClickListener) {
sender.onClick?(sender.clickedObject)
}
The ClickListener class :
class ClickListener: UITapGestureRecognizer {
var onClick : ((_: Any?) -> Void)?
var clickedObject: Any?
init(target: Any?, action: Selector?, clickedObject: Any? = nil, onClick: ((_: Any?) -> Void)? = nil) {
self.clickedObject = clickedObject
self.onClick = onClick
super.init(target: target, action: action)
}
}
And here how I call it in my view :
class VoteBoxView: UIView {
private var updateVoteButton: UILabel! = UILabel()
override init(frame: CGRect) {
super.init(frame: .zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func initLayout() {
self.addSubview(updateVoteButton)
self.updateVoteButton.text = "Modifier"
self.updateVoteButton.setToSecondaryText()
self.updateVoteButton.setToSecondaryTextColor()
self.updateVoteButton.underline()
self.updateVoteButton.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(self).offset(15)
make.right.equalTo(self)
}
self.initActions()
}
func initActions() {
updateVoteButton.isUserInteractionEnabled = true
updateVoteButton.setOnClickListener(action: updateVote, clickedObject: nil)
}
func updateVote(clickedObject: Any?) {
self.toggleResults(active: false)
}
}
I definitely succeeded to make it work in other cases but there is no differences I can find that would cause it not to work here.

The problem was that I didn't set the correct constraints on my view so the tap was blocked by my ViewController view. I found out by adding colored background on everything.

Related

How can I detect a tap event in a child class of UIView?

I'm creating an app with swift.
I've made child classes from UIView. After making them and writing some processes there, I feel that I want them to detect touch events.
But they aren't children of UIButton.
I'd not like to force them to detect touch events using UIGestureRecognizer. Because UIGestureRecognizer needs to be used in UIViewController. I'd like to write codes of detecting touch just in the view.
Are there any ways to detect touch events just in UIView?
You can simply add the gesture to the subclass of UIView as other said, but if you want to include the gesture within the definition of the subclass and make it more modular, you can use the notification dispatch mechanism to broadcast the gesture to the registered view controller.
First, you create a name for the notification:
extension Notification.Name {
static let CustomViewTapped = Notification.Name("CustomViewTapped")
}
Then, you add the gesture to your custom view:
class CustomView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
let tap = UIGestureRecognizer(target: self, action: #selector(tapped))
self.addGestureRecognizer(tap)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#objc func tapped(_ sender: UIGestureRecognizer) {
NotificationCenter.default.post(name: .CustomViewTapped, object: self)
}
}
And, finally, observe the broadcast from your view controller:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(customViewTapped), name: .CustomViewTapped, object: nil)
let customView = CustomView()
self.view.addSubview(customView)
}
#objc func customViewTapped(_ sender: UIGestureRecognizer) {
}
}
You can add UIGestureRecognizer to UIView. Another way you can add invisible UIButton on top of your UIView.
If you use UIView subclass, you can use something following and handle tap in action closure
class TappableView: UIView {
var action: (()->())? = nil
init(frame: CGRect) {
super.init(frame: frame)
initialization()
}
func initialization() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
addGestureRecognizer(tapGesture)
}
#objc private func tapGesture() {
action?()
}
}
class childView: UIView {
var action: (()->())? = nil
init(frame: CGRect) {
super.init(frame: frame)
initialization()
}
func initialization() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
addGestureRecognizer(tapGesture)
}
#objc private func tapGesture() {
//Action called here
}
}
//MARK:- View Tap Handler
extension UIView {
private struct OnClickHolder {
static var _closure:()->() = {}
}
private var onClickClosure: () -> () {
get { return OnClickHolder._closure }
set { OnClickHolder._closure = newValue }
}
func onTap(closure: #escaping ()->()) {
self.onClickClosure = closure
isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(onClickAction))
addGestureRecognizer(tap)
}
#objc private func onClickAction() {
onClickClosure()
}
}
Usage:
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView(frame: .init(x: 0, y: 0, width: 80, height: 50))
view.backgroundColor = .red
view.onTap {
print("View Tapped")
}
}

How to pass a closure as action to tap gesture added to a UIView?

I want a on click function on a UIView, but I don't know how can I pass in that closure in the first picture in the function:
First of all: I would highly recommend to add code snippets instead of screenshot in your question(s).
You need to pass a Selector for the action parameter in UITapGestureRecognizer instead of () -> () closure:
extension UIView {
func onClick(target: Any, _ selector: Selector) {
isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: target, action: selector)
addGestureRecognizer(tap)
}
}
Also, keep in mind that at this point you have to set the appropriate target for the tap instance, which means it is not self in the UIView extension (as implemented in your code); Instead, you have to pass it as an argument to the onClick method.
Usage:
In your ViewController:
likesImg.onClick(target: self, #selector(likesImgClicked))
#objc private func likesImgClicked() {
print(#function)
}
When saying likesImg.onClick(target: self: self here means the ViewController itself, not the UIView extension, that's the correct target because likesImgClicked implemented in the ViewController but not in the UIView extension.
UPDATE:
If you are insist to the approach of passing a closure, you could follow this solution:
Implement your UIView extension as:
extension UIView {
private struct OnClickHolder {
static var _closure:()->() = {}
}
private var onClickClosure: () -> () {
get { return OnClickHolder._closure }
set { OnClickHolder._closure = newValue }
}
func onClick(target: Any, _ selector: Selector) {
isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: selector)
addGestureRecognizer(tap)
}
func onClick(closure: #escaping ()->()) {
self.onClickClosure = closure
isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(onClickAction))
addGestureRecognizer(tap)
}
#objc private func onClickAction() {
onClickClosure()
}
}
Usage:
In your ViewController:
likesImg.onClick {
print("Hello!!")
}
IMPORTANT:
Thanks to #Josh Caswell for the following note:
Note that the private struct gets you one storage location for the
entire program. If you try to set a handler on more than one view,
the second will overwrite the first.
I just reinvented the swift click listener android style, based on some answers. It's very simple to use:
yourView.setClickListener {
// do some actions here!
}
Add this to your extensions file:
final class ClickListener: UITapGestureRecognizer {
private var action: () -> Void
init(_ action: #escaping () -> Void) {
self.action = action
super.init(target: nil, action: nil)
self.addTarget(self, action: #selector(execute))
}
#objc private func execute() {
action()
}
}
extension UIView {
func setClickListener(_ action: #escaping () -> Void) {
self.isUserInteractionEnabled = true
let click = ClickListener(action)
self.addGestureRecognizer(click)
}
}
I am using Xcode 11.7 and Swift 5.2
I have created a common Button action. Please refer below code
import UIKit
func customBarButton(_ target: Any, selector: Selector, controlEvent: UIControl.Event, buttonImage: String) -> UIBarButtonItem{
let customButton = UIButton(type: .custom)
customButton.setImage(UIImage(named: buttonImage), for: .normal)
customButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
customButton.addTarget(target, action: selector, for: controlEvent)
let filterItem = UIBarButtonItem(customView: customButton)
return filterItem
}
I have used the above code in my viewcontroller files.
initialize in viewdidload.
let searchItem = customBarButton(self, selector: #selector(didTapSearchButton(_:)), controlEvent: .touchUpInside, buttonImage: "search")
navigationItem.rightBarButtonItems = [searchItem]
the function called on Touch of button:
#objc func didTapSearchButton(_ sender: Any){
//enter code here
}

How to set A subclass UILabel text ? THREAD ERROR

I'm trying to set a UILabel text as follows (All the following code is swift3)
// button Action which select a random text
#IBAction func getResultButton(_ sender: TransitionButton) {
if( typeTextField.text == "ABC" && pickedCompanyField.text != "XYZ"){
// query
sender.startAnimation()
// sleep(5)
sender.stopAnimation(animationStyle: .normal)
} else if( typeTextField.text == "DEF" && pickedCompanyField.text != "XYZ"){
// query
sender.startAnimation()
sender.stopAnimation(animationStyle: .shake)
// DispatchQueue.main.async {
self.resultLabel.text = "56.36"
// }
}
}
and I use the following subclass
import UIKit
class CpLabel: UILabel {
override public var canBecomeFirstResponder: Bool {
get {
return true
}
}
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
func sharedInit() {
isUserInteractionEnabled = true
addGestureRecognizer(UILongPressGestureRecognizer(
target: self,
action: #selector(showMenu(sender:))
))
}
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
UIMenuController.shared.setMenuVisible(false, animated: true)
}
func showMenu(sender: Any?) {
becomeFirstResponder()
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(copy(_:)))
}
}
I get this Thread Error ... I don't know what is causing it ? I have tried to use DispatchQue... function to see if the error regarding main thread stuff but it seems not ? SO Any? Help will be appreciated
Error screenshot:
thanks guys I found the issue ... It was declared as UILabel and its sub classed to CpLabel , So I changed the deceleration it to CpLabel and it worked..

Does UILabel have any value to make it selectable?

Does UILabel have any value that can be set in order to make it selectable?
I have a label that I want to be selectable, (long press and a copy btn shows up) kinda like in Safari.
Self-contained Solution (Swift 5)
You can adapt the solution from #BJHSolutions and NSHipster to make the following self-contained SelectableLabel:
import UIKit
/// Label that allows selection with long-press gesture, e.g. for copy-paste.
class SelectableLabel: UILabel {
override func awakeFromNib() {
super.awakeFromNib()
isUserInteractionEnabled = true
addGestureRecognizer(
UILongPressGestureRecognizer(
target: self,
action: #selector(handleLongPress(_:))
)
)
}
override var canBecomeFirstResponder: Bool {
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(copy(_:))
}
// MARK: - UIResponderStandardEditActions
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
}
// MARK: - Long-press Handler
#objc func handleLongPress(_ recognizer: UIGestureRecognizer) {
if recognizer.state == .began,
let recognizerView = recognizer.view,
let recognizerSuperview = recognizerView.superview {
recognizerView.becomeFirstResponder()
UIMenuController.shared.setTargetRect(recognizerView.frame, in: recognizerSuperview)
UIMenuController.shared.setMenuVisible(true, animated:true)
}
}
}
Yes, you need to implement a UIMenuController from a long press gesture applied to your UILabel. There is an excellent article about this on NSHipster, but the gist of the article is the following.
Create a subclass of UILabel and implement the following methods:
override func canBecomeFirstResponder() -> Bool {
return true
}
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
return (action == "copy:")
}
// MARK: - UIResponderStandardEditActions
override func copy(sender: AnyObject?) {
UIPasteboard.generalPasteboard().string = text
}
Then in your view controller, you can add a long press gesture to your label:
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPressGesture:")
label.addGestureRecognizer(gestureRecognizer)
and handle the long press with this method:
func handleLongPressGesture(recognizer: UIGestureRecognizer) {
if let recognizerView = recognizer.view,
recognizerSuperView = recognizerView.superview
{
let menuController = UIMenuController.sharedMenuController()
menuController.setTargetRect(recognizerView.frame, inView: recognizerSuperView)
menuController.setMenuVisible(true, animated:true)
recognizerView.becomeFirstResponder()
}}
NOTE: This code is taken directly from the NSHipster article, I am just including it here for SO compliance.
UILabel inherits from UIView so you can just add a long press gesture recognizer to the label. Note that you have to change isUserInteractionEnabled to true, because it defaults to false for labels.
import UIKit
class ViewController: UIViewController {
let label = UILabel()
override func viewDidLoad() {
view.addSubview(label)
label.text = "hello"
label.translatesAutoresizingMaskIntoConstraints = false
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressLabel(longPressGestureRecognizer:)))
label.addGestureRecognizer(longPressGestureRecognizer)
label.isUserInteractionEnabled = true
}
#objc private func longPressLabel (longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == .began {
print("long press began")
} else if longPressGestureRecognizer.state == .ended {
print("long press ended")
}
}
}
I've implemented a UILabel subclass that provides all of the functionality needed. Note that if you're using this with interface builder, you'll need to adjust the init methods.
/// A label that can be copied.
class CopyableLabel: UILabel
{
// MARK: - Initialisation
/// Creates a new label.
init()
{
super.init(frame: .zero)
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(_:)))
self.addGestureRecognizer(gestureRecognizer)
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
// MARK: - Responder chain
override var canBecomeFirstResponder: Bool
{
return true
}
// MARK: - Actions
/// Method called when a long press is triggered.
func handleLongPressGesture(_ gestuerRecognizer: UILongPressGestureRecognizer)
{
guard let superview = self.superview else { return }
let menuController = UIMenuController.shared
menuController.setTargetRect(self.frame, in: superview)
menuController.setMenuVisible(true, animated:true)
self.becomeFirstResponder()
}
override func copy(_ sender: Any?)
{
UIPasteboard.general.string = self.text
}
}

Selectable UILabel contents

I have a list of URL addresses in a iOS Label object. It does not appear that the user can select and copy an item from the list and paste it into their Safari browser on the iOS device. Is there a way to accomplish this?
This feature is not there in UILabel.
You need to use UITextField or UITextView . Also do not forget to change its appearance and using
[... setEditable:NO];
It is actually possible to do with an UILabel, only you'll have to do some subclassing.
End result:
when your user long presses the label, he or she will see a copy-balloon.
Here are the steps to allow for making a label copy-able (as I can recall):
subclass UILabel
set userInteractionEnabled = YES
override canBecomeFirstResponder and return true
add a UILongPressGestureRecognizer
become first responder & present UIMenuController
Swift 3:
let menu = UIMenuController.shared
if !menu.isMenuVisible {
self.becomeFirstResponder()
menu.setTargetRect(self.bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
override canPerformAction to allow copy
Swift 3:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(UIResponderStandardEditActions.copy(_:))
}
override copy method, UIPasteboard the text & hide UIMenuController
Swift 3:
let menu = UIMenuController.shared
let labelText = self.text ?? self.attributedText?.string
if let uLabelText = labelText {
let clipBoard = UIPasteboard.general
clipBoard.string = uText
}
menu.setMenuVisible(false, animated: true)
Here is swift 5 version of JoriDor's solution;
class CopyableLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
sharedInit()
}
func sharedInit() {
isUserInteractionEnabled = true
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(showMenu(sender:))))
}
#objc
func showMenu(sender: AnyObject?) {
becomeFirstResponder()
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
}
override var canBecomeFirstResponder: Bool {
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.copy(_:)) {
return true
}
return false
}
override func copy(_ sender: Any?) {
let board = UIPasteboard.general
board.string = text
let menu = UIMenuController.shared
menu.setMenuVisible(false, animated: true)
}
}

Resources