iphone - UIGestureRecognizer 在 iPhone 中无法正常工作

标签 iphone ios uigesturerecognizer

我正在使用 UIPanGestureRecognizer 在 View 中移动 ImageView ,并使用 UISwipeGestureRecognizer 从 View 中删除 ImageView 。这是我的代码。

- (void)viewDidLoad
{

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRecognizer.delegate = self;
[imgView1 addGestureRecognizer:panRecognizer];
[panRecognizer release];


UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRecognizer.delegate = self;
swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | 
                             UISwipeGestureRecognizerDirectionRight);
[imgView1 addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
}

-(void)handleSwipe:(UITapGestureRecognizer *)recognizer{
NSLog(@"swipe");
UIView *viewGes = [recognizer view];

[viewGes removeFromSuperview];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

NSLog(@"handlePan");
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if (recognizer.state == UIGestureRecognizerStateEnded) {

    CGPoint velocity = [recognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200;

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                     recognizer.view.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        recognizer.view.center = finalPoint;
    } completion:nil];
}
}

但我的问题是滑动手势无法正常工作。有时它会接到电话,有时则不会。有时滑动移动图像然后它也删除图像。有没有人知道如何正确处理这两种手势?

最佳答案

确保你的类符合 <UIGestureRecognizerDelegate>协议(protocol)。

尝试添加此委托(delegate)方法(到您使用此识别器的类):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

关于iphone - UIGestureRecognizer 在 iPhone 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10946875/

相关文章:

ios - MapBox 缩放行为以围绕固定点缩放

ios - IAP 被拒绝,因为未提交所需的二进制文件问题

iphone - 核心数据获取结果顺序

iphone - 我无法进行应用程序购买测试

ios - 如何在 React Native 中处理图像序列

ios - Swift 中泛型和 AnyObject 的区别

ios - 从第二个 View Controller ios 将对象添加到我的基本 View Controller 中的 NSMutableArray

iphone - 使用 UIImageWriteToSavedPhotosAlbum 保存图像时无法识别的选择器错误

ios - 带有点击事件的 UITapGestureRecognizer

ios - 无法在 XCode iOS 中将系统路径设置为 header 搜索路径