Мне нужно узнать, в каком направлении было проведено смахивание, и узнать, какой объект в моем представлении отправил его. На данный момент я нашел обходной путь, но он выглядит сложным и чудовищным, поэтому я сомневаюсь, что есть простейшее решение? Мое обходное решение в двух словах:
- Я прикрепил распознаватели к каждому объекту UIButton в моем представлении.
- Есть индикатор, показывающий, какая кнопка была нажата во время свайпа.
- Я должен проверить этот индикатор и провести пальцем по экрану, чтобы принять правильное решение, какой объект и в каком направлении его следует переместить.
Спасибо за совет.
@synthesize swipeDirection; //label which shows swipe direction @synthesize buttonNumber; //label which shows number of button which being tapped - (void)viewDidLoad { UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //creating button [button setTitle:@"test1" forState:UIControlStateNormal]; button.frame = CGRectMake(100, 100, 100, 100); [button addTarget:self //adding selector which will update indicator which button been tapped, there isn't way to make swipe without tap button action:@selector(updateButtonNumber:) forControlEvents:UIControlEventTouchDown]; [button setTag:1]; [self.view addSubview:button]; //add button to view [self beginListenToSwipes:button]; //send button to method which will add UISwipeGestureRecognizer for 4 directions /*same task for second button*/ UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1 setTitle:@"test2" forState:UIControlStateNormal]; button1.frame = CGRectMake(200, 200, 100, 100); [button1 addTarget:self action:@selector(updateButtonNumber:) forControlEvents:UIControlEventTouchDown]; [button1 setTag:2]; [self.view addSubview:button1]; [self beginListenToSwipes:button1]; } /*every time when we tap button (begin swipe this method is called and buttonNumber always points to current button*/ -(void)updateButtonNumber:(UIButton*)sender { self.buttonNumber.text = [NSString stringWithFormat:@"%d",sender.tag]; } /*method receives UIButton and add to it recognizers*/ - (void) beginListenToSwipes:(UIButton*)sender { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [sender addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [sender addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [sender addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; [sender addGestureRecognizer:recognizer]; } /*method which called when swipe performed and shows in which direction it was performed*/ -(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer { int i = recognizer.direction; self.swipeDirection.text = [NSString stringWithFormat:@"%d",i]; }