Swift - How to add tap gesture to array of UIViews?
You need
@objc func tapCard (sender: UITapGestureRecognizer) {
let clickedView = cardView[sender.view!.tag]
print("View tapped !" , clickedView )
}
No need to check state here as the method with this gesture type is called only once , also every view should have a separate tap so create it inside the for - loop
for index in cardView.indices {
let tap = UITapGestureRecognizer(target: self, action: #selector(tapCard(sender: )))
How to Implement Tap Gesture on programmatically generated UIView inside UIScrollView in Swift 5
For anyone who might have same issue. Problem was with the gestures Context "self" as explained in detail here https://stackoverflow.com/a/53717065/2448688
Solved it by initializing Gesture as follows
var mediaTapRecognizer : UITapGestureRecognizer {
let t = UITapGestureRecognizer(target: self, action: #selector(openMedia))
return t
}
add tap gesture to a UILabel Programmatically
You're doing a couple things wrong...
// your code
label.frame.size.width = self.otherlinksStack.bounds.width
label.sizeToFit()
If you're adding the label to a stackView, there is no need to set its frame -- let the stack view handle that. If your stackView's alignment is set to .fill
it will stretch the label to its width anyway. If it's not set to fill, the label will expand horizontally as needed, based on its text. So, also, no need to call .sizeToFit()
.
// your code
self.otherlinksStack.addSubview(label)
When add a view to a stack view, use .addArrangedSubview
, otherwise it will be added overlaid on top of another view in the stack view.
This should work fine (it does in my quick test):
func createLabel() {
let label = UILabel()
label.text = "abc"
label.numberOfLines = 0
label.font = label.font.withSize(17) // my UIFont extension
label.tag = 1
// give the label a background color so we can see it
label.backgroundColor = .cyan
// enable user interaction on the label
label.isUserInteractionEnabled = true
// add the label as an Arranged Subview to the stack view
self.otherlinksStack.addArrangedSubview(label)
// create the gesture recognizer
let labelTapGesture = UITapGestureRecognizer(target:self,action:#selector(self.doSomethingOnTap))
// add it to the label
label.addGestureRecognizer(labelTapGesture)
}
@objc func doSomethingOnTap() {
print("tapped")
}
How to add a touch event to a UIView?
In iOS 3.2 and higher, you can use gesture recognizers. For example, this is how you would handle a tap event:
//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
//Do stuff here...
}
There are a bunch of built in gestures as well. Check out the docs for iOS event handling and UIGestureRecognizer
. I also have a bunch of sample code up on github that might help.
How to trigger tap gesture recognizer of UIView programmatically
I solved this by creating fake touches. Triggering gesture was not possible as gestures does not expose its action and selector. But as I have UIView
on which I want to tap. I can create a fake touch on that UIView
. This fake touch will automatically trigger the tap gesture. This link was very helpful for this.
Why isn't my gesture recognizer working when I programmatically create it?
It looks like you can't create a target action referencing self at initialization time of a class where self isn't available yet. I'm not sure why there's not a warning or error for this.
There are two possible workarounds:
Although it's missing from the UIGestureRecognizer documentation there is a vanilla init method with no parameters. Use that and then call
UIGestureRecognizer.addTarget(_:action:)
sometime after initialization.Use a lazy property
private lazy var tapGestureRecognizer: UITapGestureRecognizer = { .init(target: self, action: #selector(myAction)) }()
Programmatically Tap View
Just call genderTapped
directly, handing it the gesture recognizer already attached to the desired "button".
For example, if thisGenderButton
is the one you want to "tap", say:
if let tap = thisGenderButton.gestureRecognizers?[0] as? UITapGestureRecognizer {
genderTapped(tap)
}
Related Topics
iOS 13: Swift - 'Set Application Root View Controller Programmatically' Does Not Work
How to Combine React Native with Socket.Io
Swift 3 - How to Verify Class Type of Object
Nsurlsession: How to Increase Time Out for Url Requests
Why Is Main Window of Type Double Optional
Store [String] in Nsuserdefaults
Uigraphicsgetimagefromcurrentimagecontext Memory Leak with Previews
How to Fix Xcode "Dtassetproviderservice Could Not Start.." Error
Custom Annotation View for Userlocation Not Moving the Mapview
Find Object with Property in Array
How to Format a Double into Currency - Swift 3
Objective-C Declare Vars with ({ ... })
How to Make Uitextview Detect Links for Website, Mail and Phone Number
How to Pass Multiple Values with a Notification in Swift
Unable to Get the Access Token Through [Fbsdkaccesstoken Currentaccesstoken] in iPhone Sdk