当对象移动到特定帧时如何停止 UIPanGestureRecognizer

发布于 2024-12-14 16:27:02 字数 992 浏览 1 评论 0原文

我有一个使用 UIPanGestureRecognizer 移动的图像类型的对象,当该对象到达某个帧时,我需要停止识别 UIPanGestureRecognizer。

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];

-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView];

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        _firstX = [imageview center].x;
        _firstY = [imageview center].y;
    }



    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
    //NSLog(@" Move center point :%@", NSStringFromCGPoint(translatedPoint));

    [imageview setCenter:translatedPoint];  

}

我该怎么做?

I have an object of image type which I am moving using UIPanGestureRecognizer, and I need to stop recognizing the UIPanGestureRecognizer when the object reaches a certain frame.

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];

-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView];

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        _firstX = [imageview center].x;
        _firstY = [imageview center].y;
    }



    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
    //NSLog(@" Move center point :%@", NSStringFromCGPoint(translatedPoint));

    [imageview setCenter:translatedPoint];  

}

How may I do this ?

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

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

发布评论

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

评论(3

清风夜微凉 2024-12-21 16:27:05

设置pangesture.delegate=self,并实现delegate方法

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
     //return NO when you reach the frame
}

Set the pangesture.delegate=self, and implement delegate method

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
     //return NO when you reach the frame
}
瑾夏年华 2024-12-21 16:27:04

UIGestureRecognizers 有一个启用属性。文档:

禁用手势识别器,使其不接收触摸。这
默认值为“是”。如果您将此属性更改为 NO,同时
手势识别器当前正在识别一个手势,该手势
识别器转换为取消状态。

编辑:

只需将启用属性设置为“否”。

gestureRecognizer.enabled = NO;

UIGestureRecognizers have an enabled property. Documentation:

Disables a gesture recognizers so it does not receive touches. The
default value is YES. If you change this property to NO while a
gesture recognizer is currently recognizing a gesture, the gesture
recognizer transitions to a cancelled state.

EDIT:

Just set the enabled property to NO.

gestureRecognizer.enabled = NO;
擦肩而过的背影 2024-12-21 16:27:04

当您需要阻止 UIPanGestureRecognizer 识别手势时,只需将此代码行(如 jbat100 所说)放入 -(void)move:(UIPanGestureRecognizer *)gestureRecognizer 中:

gestureRecognizer.enabled = NO;

在此行之后,您的gestureRecognizer状态设置为"UIGestureRecognizerStateCancelled"

然后只需将几行添加到您的-(void)move:(UIPanGestureRecognizer *)gestureRecognizer 函数:

if ([gestureRecognizer state] == UIGestureRecognizerStateCancelled) {
     gestureRecognizer.enabled = YES;
}

您将能够使用手势识别器

编辑

这是代码片段:

- (void)move:(UIPanGestureRecognizer *)gestureRecognizer {
    BOOL cancelPanGesture = YES;
    if (cancelPanGesture) {
        /* 
         After this line gesture recognizer will be disabled, state will be UIGestureRecognizerStateCancelled
         and this method (move:) will fire one more time.
         */
        gestureRecognizer.enabled = NO;
    }
    if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
        gestureRecognizer.enabled = YES;
    }
}

When you need to stop your UIPanGestureRecognizer from recognizing gesture, you just put this code line (as jbat100 said) in -(void)move:(UIPanGestureRecognizer *)gestureRecognizer:

gestureRecognizer.enabled = NO;

after this line your gestureRecognizer state set as "UIGestureRecognizerStateCancelled"

then just add couple lines to your -(void)move:(UIPanGestureRecognizer *)gestureRecognizer function:

if ([gestureRecognizer state] == UIGestureRecognizerStateCancelled) {
     gestureRecognizer.enabled = YES;
}

and you'll be able to work with your gesture recognizer

EDIT:

Here's code snippet:

- (void)move:(UIPanGestureRecognizer *)gestureRecognizer {
    BOOL cancelPanGesture = YES;
    if (cancelPanGesture) {
        /* 
         After this line gesture recognizer will be disabled, state will be UIGestureRecognizerStateCancelled
         and this method (move:) will fire one more time.
         */
        gestureRecognizer.enabled = NO;
    }
    if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
        gestureRecognizer.enabled = YES;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文