Getting the Action of UIGestureRecognizer in iOS
There is a way to gain access to the property target
, but I'm not sure that this method will pass the Apple approval process.
NSMutableArray *targets = [myGes valueForKeyPath:@"_targets"];
id targetContainer = targets[0];//get first target for example
id targetOfMyGes = [targetContainer valueForKeyPath:@"_target"];
NSLog(@"%@", targetOfMyGes );//you can see reference for target object
Thanks neilco - his answer help create solution.
Note: the exact class of the object targetOfMyGes
need to define yourself. By default it id
- suitable for any object class.
Manipulate target of UIGestureRecognizer in action function
UIGestureRecognizer
has a view
property that returns the view it was added to.
UIGestureRecognizer not sending action to other class
You are creating that gesture inside of a method, and the gesture may only live in that scope while the function is called. So technically is disappearing before you use it. So creating the instance of that gesture outside of the method may live while the view exist. If that makes sense.
UPDATE: As pointed from the comments, we do need a current reference of the parent viewController.
How about doing it this way..
class OperationView: UIView {
let swipeGestureRecognizer = UISwipeGestureRecognizer()
let tapGestureRecognizer = UITapGestureRecognizer()
override func awakeFromNib() {
super.awakeFromNib()
if let parentViewController = parentViewController {
swipeGestureRecognizer.addTarget(parentViewController, action: #selector(HomeViewController.operatorWasSwiped(_:)))
swipeGestureRecognizer.direction = .left
tapGestureRecognizer.addTarget(parentViewController, action: #selector(HomeViewController.operatorWasSwiped(_:)))
self.addGestureRecognizer(swipeGestureRecognizer)
self.addGestureRecognizer(tapGestureRecognizer)
}
}
}
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
UIGestureRecognizer in InterfaceBuilder target/action
Did you implement -(void)longPress:(UIGestureRecognizer)recognizer in your .m? (I am suggesting updating the id -> UIGestureRecognizer and sender -> recognizer; IBAction is typedef to void but you might make it explicit)
You definitely want to implement the selector, and this type of crash is very common when the method (if implemented at all) doesn't match the selector. Even missing the colon could cause a mismatch.
Also, have you set up the debugger to break when an exception is raised? If not, go to debugger (6th "tab" from left on left pane), click + to add a new one, "Add Exception Breakpoint", and keep defaults). This will push you into the debugger as soon as an exception is raised, which is usually a useful place to be in the stack.
Good luck,
Damien
Swift3 iOS - How to make UITapGestureRecognizer trigger function
From your code you are using swift 3.0 so change your selector
syntax like this
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
and Your function like this
func tapBlurButton(_ sender: UITapGestureRecognizer) {
print("Please Help!")
}
Edit:
Not idea that you are using button with tap gesture, instead of that use inbuilt method addTarget
for button no need to create tap gesture for it like this
qsBlurButton.addTarget(self, action: #selector(self.tapBlurButton(_:)), forControlEvents: .TouchUpInside)
func tapBlurButton(_ sender: UIButton) {
print("Please Help!")
}
Related Topics
Sprite Kit Sprites Loading Performance Improvement
Uilabel, Uifont and Utf-8 Triangle
Uploading Image with iOS App to Server File Size Is Too Large
Removing Lagging Latency in Drawing Uibezierpath Smooth Lines in Swift
Hide a Phone Call Completely in iOS (Jailbreak Device)
Open Uiimagepickercontroller in Landscape Mode
How to Upload Multiple Image on Firebase Using Swift
What Is the Use of Singleton Class in Objective-C
Type 'Nsattributedstringkey' (Aka 'Nsstring') Has No Member 'Font'
Google 400 Error: Invalid Request Custom Scheme Uris Are Not Allowed for 'Web' Client Type
What Does $0 Represent in Closures in Swift
Programmatically Detect Dark Mode in Swiftui to Display Appropriate Image
Cannot Invoke 'Indexof' with an Argument List of Type '(Checklistitem)'
Offline Crash Reporting in Crashlytics
How to Hide a Bar Button Item for Certain Users
Didreceiveremotenotification Function Doesn't Called with Fcm Notification Server