简体   繁体   English

iPhone iOS UITapGestureRecognizer是否可以用某个接触点初始化识别器?

[英]iPhone iOS UITapGestureRecognizer is it possible to initialize a recognizer with a certain touch point?

I'm using a tap gesture recognizer to position a little subview with a label within another UIView. 我使用点击手势识别器在另一个UIView中放置带有标签的子视图。 When the user taps on a view, the label is populated with the title of the component that the user tapped on, and the subview is centered at the tap location. 当用户点击视图时,标签将填充有用户点击的组件的标题,子视图位于点击位置的中心。

I need to make sure that the initial position of my gesture recognizer matches the center of the subview as defined in the storyboard(before the user taps on a view), but it seems that I'm unable to find a way to pass this point over to the gesture recognizer. 我需要确保我的手势识别器的初始位置与情节提要中定义的子视图的中心匹配(在用户点击视图之前),但是似乎无法找到一种方法来传递此点转到手势识别器。 Is there a way to initialize my tap gesture recognizer with a certain point within a view? 有没有一种方法可以用视图中的特定点初始化我的点击手势识别器?

I'm not quite sure what you are asking. 我不太确定你在问什么。 Gesture Recognizes do not have a "starting point". 手势识别没有“起点”。 They receive various touch types for touches inside of a given view and allow you to uniquely handle each one. 它们为给定视图内的触摸接收各种触摸类型,并允许您唯一地处理每个视图。

If you want to simulate a touch on loading (which sort of sounds like that might be what you are looking to do), reorganize your code to be something like this: 如果要模拟加载时的感觉(可能是您想要的声音),请将代码重新组织为以下形式:

- (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
}

In this example, you could start the position/data of your subview based on what it would be if there was a touch at 100,100. 在此示例中,您可以基于在100,100触摸时的状态来开始子视图的位置/数据。

Note: I left out code configuring your gesture recognizer because it sounds like you already have that part under control. 注意:我遗漏了配置手势识别器的代码,因为听起来您已经控制了该部分。 If not, I can post with more code. 如果没有,我可以发布更多代码。

A couple things that might help: 有几件事情可能会有所帮助:

Gesture recognizers can be attached to any view in the view hierarchy. 手势识别器可以附加到视图层次结构中的任何视图。 So if you want some small sub-sub-view to recognize the tap, you can addGestureRecognizer to that view. 因此,如果您希望一些小的子视图识别出该拍子,则可以将GestureRecognizer添加到该视图。

When the gesture is recognized, you can test the gesture's location (and other aspects of it's state) before deciding to do anything about it. 识别手势后,您可以在决定对其进行任何操作之前测试手势的位置(以及其状态的其他方面)。 For example, let's say you only want the gesture to work if the user taps a very small space inside of a view... 例如,假设您只希望手势在用户点击视图内很小的空间时起作用。

- (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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 难以在iOS应用中激活3触摸手势识别器 - Difficulty activating 3 touch gesture recognizer in iOS app 使用UITapGestureRecognizer根据iPhone中的手指触摸位置添加新视图 - adding the new view according to the finger touch location in iPhone using UITapGestureRecognizer iPhone:UITableViewController中的触摸点捕获 - iPhone: touch point capture in UITableViewController 是否可以使用iPod Touch 4开发适用于iPhone和iPod Touch的iOS应用程序并发布? - It is possible to use a iPod Touch 4 to develop iOS app running and published for both iPhone and iPod Touch? iPhone iOS如何将UILongPressGestureRecognizer和UITapGestureRecognizer添加到同一控件并防止冲突? - iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict? iPhone上不规则的iOS触摸率 - Irregular iOS Touch Rate on iPhone 从您触摸它的点拖动视图 iPhone - Drag View from the point you touch it iPhone 禁用iPhone屏幕某些区域的触摸事件 - Disable touch events on certain areas of iPhone screen iOS - UITapGestureRecognizer - 带参数的选择器 - iOS - UITapGestureRecognizer - Selector with Arguments ios-UITapGestureRecognizer与UITextView - ios - UITapGestureRecognizer with UITextView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM