3

这是 UIPanGesture 子类:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began");
   self ->origLoc = [[touches anyObject] locationInView:self.view.superview];
   [super touchesBegan:touches withEvent:event];
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.state == UIGestureRecognizerStatePossible) {
    CGPoint loc = [[touches anyObject] locationInView:self.view.superview];
    CGFloat deltaX = fabsf(loc.x - origLoc.x);
    CGFloat deltaY = fabsf(loc.y - origLoc.y);
    if (deltaY >= deltaX) {
        self.state = UIGestureRecognizerStateFailed;
    } else {
    [super touchesMoved:touches withEvent:event];
    }
}
}

- (CGPoint) translationInView:(UIView *)v {

CGPoint proposedTranslation = [super translationInView:v];
proposedTranslation.y = 0;
return proposedTranslation;

}

这是我的子类 UIPanGesture 调用的方法:

- (void)dragging: (UIPanGestureRecognizer *)p {    

UIView *vv = p.view;

if (p.state == UIGestureRecognizerStateBegan || p.state == UIGestureRecognizerStateChanged) {
    CGPoint delta = [p translationInView:vv.superview];
    CGPoint c = vv.center;
    c.x += delta.x;
    c.y += delta.y;
    vv.center = c;
    [p setTranslation:CGPointZero inView:vv.superview];
 }    
}

当我试图拖动我的视图时,它只是移动了一个非常小的量然后停止。我需要继续轻推它才能让它移动。有任何想法吗?

提前致谢,

迈克尔。

4

3 回答 3

2

在我之前的评论中,我不清楚您为什么要进行子类化,但您澄清了这是因为您有多个手势处理程序正在进行。很好。

因此,这里有一些自定义平移手势的示例代码,Horizo​​ntalPanGestureRecognizer,仅当第一个移动是水平的时才会触发:

// HorizontalPanGestureRecognizer.h

#import <UIKit/UIKit.h>

@interface HorizontalPanGestureRecognizer : UIPanGestureRecognizer
@end

// HorizontalPanGestureRecognizer.m

#import "HorizontalPanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface HorizontalPanGestureRecognizer ()
{
    BOOL _hasConfirmedDirection;
    BOOL _wasHorizontal;
    CGPoint _origLoc;
}
@end

@implementation HorizontalPanGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    _hasConfirmedDirection = NO;

    _origLoc = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    if (self.state == UIGestureRecognizerStateFailed) return;

    if (!_hasConfirmedDirection)
    {
        CGPoint translation = [self translationInView:self.view];

        _hasConfirmedDirection = YES;
        _wasHorizontal = (fabs(translation.x) > fabs(translation.y));
    }
    if (!_wasHorizontal)
        self.state = UIGestureRecognizerStateFailed;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    if (!_wasHorizontal)
        self.state = UIGestureRecognizerStateFailed;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];

    if (!_wasHorizontal)
        self.state = UIGestureRecognizerStateFailed;
}

- (CGPoint)locationInView:(UIView *)view
{
    CGPoint superLocation = [super locationInView:view];

    if (_hasConfirmedDirection)
        superLocation.y = _origLoc.y;

    return superLocation;
}

- (CGPoint)translationInView:(UIView *)view
{
    CGPoint superTranslation = [super translationInView:view];

    if (_hasConfirmedDirection)
        superTranslation.y = 0.0f;

    return superTranslation;
}

@end

然后你可以让你的主视图控制器中的处理程序适当地使用它(在这个例子中,只是拖动一个 UILabel )。在 viewDidLoad 中,创建手势:

HorizontalPanGestureRecognizer *recognizer = [[HorizontalPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCustomPan:)];
[self.view addGestureRecognizer:recognizer];

然后处理程序看起来像:

- (void)handleCustomPan:(UIPanGestureRecognizer *)sender 
{    
    switch (sender.state) {
        case UIGestureRecognizerStateChanged:
            if (!_panLabel)
            {
                // In my case I'm creating a UILabel to drag around, whereas you might just drag
                // whatever countrol you want to drag.
                //
                // But, regardless, I'm keeping track of the original center in _panLabelOrigCenter

                [self makePanLabel:sender]; 
            }

            CGPoint translate = [sender translationInView:self.view];
            _panLabel.center = CGPointMake(_panLabelOrigCenter.x + translate.x, _panLabelOrigCenter.y + translate.y);

            break;

        case UIGestureRecognizerStateEnded:
            [self removePanLabel];
            break;

        default:
            break;
    }
}

(显然,这是 ARC 代码。如果不是 ARC,请添加必要的附加代码行以进行例行内存管理。)

于 2012-06-15T01:11:10.473 回答
0

触摸移动的失败情况看起来对早期运动非常敏感。

考虑让它不那么敏感...

- (BOOL)movementIsHorizontalX:(CGFloat)x andY:(CGFloat) y {

    CGFloat absX = fabs(x);
    CGFloat absY = fabs(y);

    // if x is small, then accept small y values but not large ones
    if (absX < 4) return absY < 4;

    // now we can express horizontal-ness as a slope.  we've ruled out small x, so this is a stable calculation
    CGFloat slope = absY / absX;
    return slope < 0.10;

    // you could generalize by passing in the x threshold and percentage slope constants
}
于 2012-06-13T06:27:22.773 回答
0

我意识到我的错误。我搬家了

    [super touchesMoved: touches withEvent:event];  

在 if 语句之外,现在它似乎工作正常。不是 100% 确定它为什么现在有效,但我很高兴它确实有效。

现在要弄清楚如何防止 UIScrollView 接受平移手势,直到这个手势取消/失败......

于 2012-06-13T19:29:13.303 回答