Getting the Action of Uigesturerecognizer in iOS

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



Leave a reply



Submit