ios - 为什么我的平移手势只会轻推其 View 并停止?

标签 ios uipangesturerecognizer

这是 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];
 }    
}

当我尝试拖动 View 时,它只移动了很小的量,然后就停止了。我需要不断地轻推它才能让它移动。有什么想法吗?

提前致谢,

迈克尔。

最佳答案

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

这里是自定义平移手势 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

然后您可以让主视图 Controller 中的处理程序适本地使用它(在本示例中,只需拖动 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,请添加必要的附加代码行以进行常规内存管理。)

关于ios - 为什么我的平移手势只会轻推其 View 并停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009154/

相关文章:

ios - 将 Pan UIGesture 转换为滑动手势

swift - 在 contentView 中的 sideView 上禁用手势

objective-c - UIPanGestureRecognizer 与 UISwipeGestureRecognizer 重叠

ios - Swift 2.1 与 removeFromSuperview 相反

ios - Swift:处理自定义幻灯片菜单中的快速滑动

ios - QLPreviewController 和 TouchBegan 方法

ios - 每周自动更新标签

iOS,在 UIImage 上绘制带笔划的文本

iphone - iOS 应用程序针对 Facebook 集成配置错误

iphone - 有没有办法获取在我的 UIWebView 中播放的视频的长度?我可以在某个位置开始我的电影吗?