问题
I am creating a Share extension and faced with a strange behaviour during my tests on iOS 13.0 and later. I use UISwipeGestureRecognizer to interpret user's swiping gestures on the main view on my extension.
This simple code provides below as an example of that I want and works perfectly on 12.4 and older:
@interface ShareAndSwipeRootController ()
@end
@implementation ShareAndSwipeRootController
- (void)loadView {
[super loadView];
[self.view setBackgroundColor:[UIColor redColor]];
[self.view setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUp:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUpGestureRecognizer];
UISwipeGestureRecognizer *swipeDownGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDown:)];
swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDownGestureRecognizer];
};
-(void) swipeUp:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"SWIPE Up");
}
-(void) swipeDown:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"SWIPE Down");
}
@end
On iOS 13.0 and newer it logs nothing. You might check the difference on iOS Simulator for corresponding versions.
Perhaps someone solved this problem and knows what is the reason or found its description - please share the result.
Thanks.
回答1:
You need to inspect .gestureRecognizers property in order to check what went wrong or something unusual happens.
As it is a server Gesture Recognition. you need to try shouldRecognizeSimultaneouslyWith
method as written below:
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
If everything goes well it'll written True.
回答2:
Vlad, that code works fine on my simulator and device (13.5) but I do suggest you do it differently.
It is a bit heavy handed to implement loadView
and if you do you should not call super
in this method.
Why not move your code as is into viewDidLoad
where you normally attach gestures? So remove loadView
and do
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor redColor]];
[self.view setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUp:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUpGestureRecognizer];
UISwipeGestureRecognizer *swipeDownGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDown:)];
swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDownGestureRecognizer];
};
来源:https://stackoverflow.com/questions/62019991/uiswipegesturerecognizer-and-shareextension-different-behaviour-on-ios-12-4-and