0

我正在使用 UIPanGestureRecognizer 在视图中移动 imageview 并使用 UISwipeGestureRecognizer 从视图中删除 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];
}
}

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

4

2 回答 2

2

Make sure your class conforms to <UIGestureRecognizerDelegate> protocol.

Try adding this delegate method (to the class where you use this recognizers):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
于 2012-06-08T10:16:40.050 回答
0

仔细检查您的所有图像视图用户交互是否已启用:

[imgView1 setUserInteractionEnabled:YES];
于 2013-06-23T08:56:48.087 回答