UIGestureRecognizer 动作限制

发布于 2024-10-01 17:59:32 字数 275 浏览 0 评论 0原文

我想知道是否有办法限制 GestureRecognizers 中 UIView 的大小或移动。

当然,我可以编写自己的限制代码,但我想知道我们是否可以设置某些属性或某些东西来具有一些限制。

例如,在 UIPanGestureRecognizer 中,我想限制添加到 PanGesture 的视图在特定 CGRect 内的移动,那么有什么方法可以设置该边界矩形来感应 < code>PanGesture 如果视图超出我的边界框,它不会允许它。

I want to know if there is a way to restrict the size or movements of the UIViews in the GestureRecognizers.

Sure I could write my own restriction code but wana know if we can set some property or something to have some bound limits.

For example in UIPanGestureRecognizer I want to restrict the movement of the view added to PanGesture within a particular CGRect, so is there any way I can set that bounding rect so on sensing PanGesture if the view is going out of my bounding frame it wont allow it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-10-08 17:59:32

据我所知,你必须自己写。

点击手势可以让你设置用多少根手指响应多少次点击,但大多数手势并没有提供很多帮助。

as far as I am aware you have to write that yourself.

tap gestures let you set how many taps you are responding to with how many fingers, but most of them do not provide a lot of helpers.

亚希 2024-10-08 17:59:32

我认为完成此操作的最简单方法是在该矩形中创建一个不可见视图并向其添加手势。
例子:
您想要检测视图 X 中矩形 {a,b,c,d} 内的移动。
在 {a,b,c,d} 矩形中创建一个 UIView Y。将其背景颜色设置为clearColor。
创建手势识别器并将其添加到 Y 视图。
在手势的选择器中执行如下操作:

- (void) selector:(UIPanGestureRecognizer *) gesture
{
    UIView *viewX = [self.view viewWithTag:kTagViewX];
    //now you know that the gesture took place and you have access to your view
}

如果您需要更改希望手势处于活动状态的矩形,只需移动 Y 视图即可!

编辑:

您还可以使用 UIGestureRecognizer 委托并在此方法中返回 TRUE 或 FALSE:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

取决于该触摸参数的位置。
实际上,这是苹果公司在其 WWDC 会议视频之一中建议的方式。而且它更好,因为您不必添加另一个视图并填满内存。

I think the simplest way to accomplish this is to create an invisible view in that rect and add the gesture to it.
Example:
You want to detect the movement in view X inside the rect {a,b,c,d}.
Create a UIView Y in the {a,b,c,d} rect. Set it's background color to clearColor.
Create the gesture recognizer and add it to the Y view.
In the gesture's selector do something like this:

- (void) selector:(UIPanGestureRecognizer *) gesture
{
    UIView *viewX = [self.view viewWithTag:kTagViewX];
    //now you know that the gesture took place and you have access to your view
}

If you need to change the rect in which you want the gesture to be active , just move the Y view and voila!

EDIT:

You can also use the UIGestureRecognizer delegate and return TRUE or FALSE in this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

depending on the position of that touch argument.
Actually this is the way Apple suggest's in one of their WWDC session videos. And it's better since you don't have to add another view and fill up the memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文