I want to implement UIGestureRecognizer in a different class

Asked 2 years ago, Updated 2 years ago, 74 views

I'd like to implement gestures on a ViewController.

UISwipeGestureRecognizer*gesture=[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(getSwipe:)];

gesture.direction =UISwipeGestureRecognizerDirectionDown;

self.view addGestureRecognizer:gesture;

Is it possible to have these methods in this ViewController's own class called GestureManager?

ios objective-c

2022-09-30 20:57

2 Answers

It seems that the only way to do it is to say, "It depends on how you do it."
The point is this code.

[self.view addGestureRecognizer:gesture];

UIGestureRecognizer (subclass in ) works for the first time by add to UIView (subclass in ).If you don't forget this, your implementation won't fail.


2022-09-30 20:57

Can't I do this?

UISwipeGestureRecognizer*gesture=[[UISwipeGestureRecognizer alloc] initWithTarget: yourManager action:@selector(getSwipe:)];

gesture.direction =UISwipeGestureRecognizerDirectionDown;

self.view addGestureRecognizer:gesture;


2022-09-30 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.