【问题标题】:Recognize swipe direction together with UIButton object与 UIButton 对象一起识别滑动方向
【发布时间】:2012-05-12 23:27:01
【问题描述】:

我需要识别执行滑动的方向并知道我视图中的哪个对象发送了它。我现在找到了解决方法,但它看起来很复杂而且很可怕,所以我怀疑有没有最简单的解决方案? 我的解决方法是几句话:

  1. 我将识别器附加到视图上的每个 UIButton 对象。
  2. 有指示器显示在执行滑动过程中点击了哪个按钮
  3. 我必须检查此指示器并滑动方向才能做出正确的决定,即应该移动哪个对象以及它应该向哪个方向移动。

谢谢你的建议。

@synthesize 滑动方向; //显示滑动方向的标签 @synthesize 按钮编号; //标签显示被点击的按钮数量 - (void)viewDidLoad { UIButton* 按钮 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //创建按钮 [按钮 setTitle:@"test1" forState:UIControlStateNormal]; button.frame = CGRectMake(100, 100, 100, 100); [button addTarget:self //添加选择器,它将更新指示器被点击的按钮,没有点击按钮无法滑动 动作:@选择器(更新按钮编号:) forControlEvents:UIControlEventTouchDown]; [按钮设置标签:1]; [self.view addSubview:button]; //添加按钮查看 [self beginListenToSwipes:button]; //向方法发送按钮,该方法将为 4 个方向添加 UISwipeGestureRecognizer /*第二个按钮的相同任务*/ UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1 setTitle:@"test2" forState:UIControlStateNormal]; button1.frame = CGRectMake(200, 200, 100, 100); [button1 addTarget:自我 动作:@选择器(更新按钮编号:) forControlEvents:UIControlEventTouchDown]; [button1 setTag:2]; [self.view addSubview:button1]; [self beginListenToSwipes:button1]; } /*每次我们点击按钮(开始滑动时调用此方法并且buttonNumber总是指向当前按钮*/ -(void)updateButtonNumber:(UIButton*)sender { self.buttonNumber.text = [NSString stringWithFormat:@"%d",sender.tag]; } /*方法接收 UIButton 并添加识别器*/ - (void) beginListenToSwipes:(UIButton*)sender { UISwipeGestureRecognizer *recognizer; 识别器 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [识别器设置方向:(UISwipeGestureRecognizerDirectionRight)]; [发送者 addGestureRecognizer:recognizer]; 识别器 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [识别器设置方向:(UISwipeGestureRecognizerDirectionLeft)]; [发送者 addGestureRecognizer:recognizer]; 识别器 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [识别器设置方向:(UISwipeGestureRecognizerDirectionUp)]; [发送者 addGestureRecognizer:recognizer]; 识别器 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [识别器设置方向:(UISwipeGestureRecognizerDirectionDown)]; [发送者 addGestureRecognizer:recognizer]; } /*滑动执行时调用的方法并显示执行的方向*/ -(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer { int i = 识别器.方向; self.swipeDirection.text = [NSString stringWithFormat:@"%d",i]; }

【问题讨论】:

    标签: objective-c ios uiswipegesturerecognizer


    【解决方案1】:

    我的想法是,

    1. 为 UIButton 注册 touchUpoutside 事件。
    2. 当您将手指移出按钮时,它将被触发。
    3. 从 touchesEnded 方法中找到接触点并存储在类级变量中。
    4. 比较touchupoutside方法中的按钮框和触摸点,找出运动方向。

    【讨论】:

    • 如果我们注册 touchUpoutside ,那么在 uibutton 上单按(touchUpInside) 是如何工作的?无论如何它都不起作用尝试过的人。 @vignesh
    【解决方案2】:

    我认为您也可以将手势识别器放在 self.view 中,然后查看手势是否发生在相关控件上(显然,您希望将按钮 ivars 或 viewcontroller 的属性设置为跟踪哪个是哪个),例如

    CGPoint point = [sender locationInView:self.view];
    if (CGRectContainsPoint(button1.frame, point))
        // do whatever you want
    

    由您决定是否在 UIGestureRecognizerStateBegan 或 UIGestureRecognizerStateEnded 或同时执行此逻辑。

    更新:

    顺便说一句,如果您要检测移动某物的方向,您还可以考虑使用 UIPanGestureRecognizer,它在单个手势识别器中处理所有四个方向(并且因为它是连续的,您可以为用户提供实时UI 中的反馈动画移动)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      相关资源
      最近更新 更多