我需要识别在哪个方向上执行滑动并知道我视图中的哪个对象已发送它。我现在找到了解决方法,但它看起来很复杂而且很可怕,所以我怀疑有没有最简单的解决方案? 用几句话解决我的问题:
谢谢你的建议。
@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]; }
答案 0 :(得分:1)
我的想法是,
touchUpoutside
事件。touchupoutside
方法中的按钮框和触摸点,以找到移动方向。答案 1 :(得分:0)
我认为你也可以将你的手势识别器放在self.view中,然后查看手势是否发生在有问题的控件上(很明显,你想要将你的按钮设为iv或者你的viewcontroller的属性所以你可以跟踪哪个是哪个),例如
CGPoint point = [sender locationInView:self.view];
if (CGRectContainsPoint(button1.frame, point))
// do whatever you want
您是否可以在UIGestureRecognizerStateBegan或UIGestureRecognizerStateEnded或两者中执行此逻辑。
<强>更新强>
顺便说一下,如果您正在检测移动某个方向的方向,您还可以考虑使用UIPanGestureRecognizer,它可以在一个手势识别器中处理所有四个方向(并且因为它是连续的,您可以在UI动画中为用户提供实时反馈。