Добавление распознавателя жестов в DetailViewContoller
У меня есть простой проект xcode, созданный только из шаблона "Master-Detail Application", для iPad. Когда устройство находится в портретной ориентации, основной вид скрыт, и когда вы проведите пальцем по экрану прямо на подробном представлении, откроется главное представление. Теперь я хочу добавить правый распознаватель жестов к подробному представлению, например:
- (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", но мастер-вид не отображается.
Как добавить признак распознавания жестов салфетки к подробному представлению, чтобы он не мешал отображению главного вида, и мой обработчик для распознавателя будет работать?
Спасибо заранее.
ИЗМЕНИТЬ. Я хочу, чтобы мой обработчик распознавания правой руки работал одновременно с этим встроенным в один, который показывает главный вид, но следующий код не является решением для этой конкретной ситуации:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
Ответы
Ответ 1
вы должны указать направление для салфетки, чтобы добавить правый сабпорт
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];
и ваш обработчик салфетки может выглядеть как
-(void)swipeHandler:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
}
Ответ 2
- (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];
}];
}
}
Ответ 3
Попробуйте это
//Справа
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
}
Ответ 4
Это работает. Он выталкивает контроллер вида из навигационного контроллера.
- (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];
}
Затем обязательно перейдите в раскадровку (или вы также можете сделать это вручную) - щелкните на контроллере подробного представления и дайте контроллеру View идентификатор: DetailViewController