我有一个简单的xcode项目,它只是用于iPad的“Master-Detail Application”模板。当设备处于纵向方向时,主视图将被隐藏,当您在详细视图上向右滑动时,将显示主视图。现在,我想将正确的滑动手势识别器添加到详细视图中,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(void)swipeHandler{
NSLog(@"SWIPE");
}
但是这段代码导致当我在详细视图上滑动时,“SWIPE”日志出现在控制台中,但主视图不会显示。
如何向细节视图添加右滑动手势识别器,因此它不会阻止主视图显示,我的识别器处理程序将起作用?
提前致谢。
EDIT。我希望我的右侧滑动识别器处理程序与此内置的一个同时工作,它显示主视图,但以下代码不是针对此特定情况的解决方案:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
答案 0 :(得分:38)
您应该设置滑动的方向以添加正确的滑动
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];
并且您的滑动处理程序可能看起来像
-(void)swipeHandler:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
}
答案 1 :(得分:18)
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizer:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight;
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];
}
- (void)swipeRecognizer:(UISwipeGestureRecognizer *)sender {
if (sender.direction == UISwipeGestureRecognizerDirectionRight){
[UIView animateWithDuration:0.3 animations:^{
CGPoint Position = CGPointMake(self.view.frame.origin.x + 100.0, self.view.frame.origin.y);
self.view.frame = CGRectMake(Position.x , Position.y , self.view.frame.size.width, self.view.frame.size.height);
[self.navigationController popViewControllerAnimated:YES];
}];
}
}
答案 2 :(得分:9)
试试这个
//右键滑动
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];
//向左滑动
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:gestureRecognizer];
通话方法
-(void)swipeHandlerRight:(id)sender
{
//Your ViewController
}
-(void)swipeHandlerLeft:(id)sender
{
//Your ViewController
}
答案 3 :(得分:0)
这很有效。它从navigationController推送视图控制器。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(IBAction)swipeHandler:(UISwipeGestureRecognizer *)sender
{
NSLog(@"SWIPE");
UIViewController *tb = [[DetailViewController alloc] init];
tb = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.navigationController pushViewController: tb animated:YES];
}
然后一定要去故事板(或者你也可以手动完成) - 点击Detail View Controller并给View控制器一个Identity:DetailViewController
答案 4 :(得分:0)
swift 4.0
步骤1:在viewDidLoad()方法中添加滑动手势。
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizerDirection.right {
print("Swipe Right")
}
else if gesture.direction == UISwipeGestureRecognizerDirection.left {
print("Swipe Left")
}
}
步骤2:检查handleGesture()方法中的手势检测
Speed up screenshot logic in UiAutomator2