【问题标题】:iPhone iOS UITapGestureRecognizer is it possible to initialize a recognizer with a certain touch point?iPhone iOS UITapGestureRecognizer 是否可以使用某个触摸点初始化识别器?
【发布时间】:2012-06-26 18:35:36
【问题描述】:

我正在使用点击手势识别器在另一个 UIView 中放置一个带有标签的小子视图。当用户点击视图时,标签会填充用户点击的组件的标题,并且子视图以点击位置为中心。

我需要确保我的手势识别器的初始位置与情节提要中定义的子视图的中心相匹配(在用户点击视图之前),但似乎我无法找到一种方法将此点传递给手势识别器。 有没有办法用视图中的某个点来初始化我的点击手势识别器?

【问题讨论】:

    标签: iphone ios uiview cgpoint uitapgesturerecognizer


    【解决方案1】:

    我不太确定你在问什么。手势识别没有“起点”。它们接收给定视图内的各种触摸类型,并允许您唯一地处理每一个。

    如果您想模拟加载时的触摸(听起来可能是您想要做的),请将您的代码重新组织为如下所示:

    - (void)viewDidLoad
    {
         //simulate touch here
         [self touchedAtLocation:CGPointMake(100, 100)];
    }
    
    //Your delegate method
    - (void)handleTap:(UITapGestureRecogizer *)recognizer
    {
        [self touchedAtLocation:[recognizer locationInView:self.view]];
    }
    
    - (void)touchedAtLocation:(CGPoint)location
    {
        //perform action based on location of touch
    }
    

    在此示例中,您可以根据触摸 100,100 时的位置/数据来启动子视图的位置/数据。

    注意:我省略了配置您的手势识别器的代码,因为听起来您已经控制了该部分。如果没有,我可以发布更多代码。

    【讨论】:

    • 迈克,感谢您的解决方案!我想初始化一个接触点的部分原因是识别器记住了这个点。我可以移动识别器触摸的视图,当我在手势识别器下移动视图时,它会自动调整触摸点。这消除了一些我还没有准备好手动进行的计算。
    【解决方案2】:

    几件事可能会有所帮助:

    手势识别器可以附加到视图层次结构中的任何视图。所以如果你想要一些小的 sub-sub-view 来识别点击,你可以在那个 view 中添加 GestureRecognizer。

    识别手势后,您可以先测试手势的位置(及其状态的其他方面),然后再决定对其执行任何操作。例如,假设您只希望手势在用户点击视图内非常小的空间时起作用......

    - (void)handleTap:(UITapGestureRecogizer *)recognizer {
    
        // get the location relative to the subview to which this recognizer is attached
        CGPoint location = [recognizer locationInView:recognizer.view];
    
        // tiny rect to test, also in the recognizer's view's coordinates
        CGRect someSmallerRect = CGRectInset(recognizer.view.bounds, 10, 10);
    
        if (CGRectContainsPoint(someSmallerRect, location)) {
            // do whatever the touch should do
        }
        // otherwise, it's like it never happened
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2020-11-13
      • 2016-03-03
      相关资源
      最近更新 更多