2

我有一个带有 3 个独立子视图的父视图。子视图在父视图中展开,没有重叠(并且中间有一些空间)。当用户在屏幕上移动她的手指(不抬起它)时,我想在他们进入和退出每个子视图时跟踪触摸。

示例:如果用户开始触摸屏幕上子视图之外的某个位置,然后在子 1 上、从子 1 上、在子 2 上滑动手指,然后松开,我希望这些事件会被触发:

  1. 触摸开始
  2. 触摸进入孩子 1
  3. 触摸退出的孩子 1
  4. 触摸进入孩子2
  5. 触摸结束

似乎 touchesBegan:withEvent: 和 touchesEnded:withEvent: 方法在这种情况下会有所帮助,但是当我在子视图控制器上定义它们时,它们并没有完全按照我的意愿做——如果用户开始触摸外部子视图,然后在子视图上滑动,子视图本身不会触发任何触摸事件。

当前解决方案:我目前正在使用一种感觉非常糟糕的解决方案来完成此任务。我在父级上观察 touchesBegan:withEvent:、touchesEnded:withEvent: 和 touchesMoved:withEvent:,获取每个事件的坐标,并确定它们是否位于子级范围内。如果他们这样做,我会触发上述适当的事件。

这种方法大多有效,但感觉效率很低。感觉框架应该为我处理这项工作。我的状态管理代码有时也会错过“进入”或“退出”触发器,我怀疑这是因为触摸事件要么被丢弃,要么以意外的顺序出现。我在这里错过了更好的方法吗?

4

1 回答 1

2

最简单的解决方案是:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];

    // Do any additional setup after loading the view.
}

- (void)pan:(UIPanGestureRecognizer *)sender
{
    static NSInteger startViewIndex;
    static NSInteger endViewIndex;
    CGPoint location = [sender locationInView:self.view];

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        if (CGRectContainsPoint(self.view0.frame, location))
            startViewIndex = 0;
        else if (CGRectContainsPoint(self.view1.frame, location))
            startViewIndex = 1;
        else if (CGRectContainsPoint(self.view2.frame, location))
            startViewIndex = 2;
        else 
            startViewIndex = -1;
    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
        if (CGRectContainsPoint(self.view0.frame, location))
            endViewIndex = 0;
        else if (CGRectContainsPoint(self.view1.frame, location))
            endViewIndex = 1;
        else if (CGRectContainsPoint(self.view2.frame, location))
            endViewIndex = 2;
        else 
            endViewIndex = -1;

        if (startViewIndex != -1 && endViewIndex != -1 && startViewIndex != endViewIndex)
        {
            // successfully moved between subviews!
            NSLog(@"Moved from %1d to %1d", startViewIndex, endViewIndex);
        }
    }
}

也许更优雅一点是定义您自己的自定义手势识别器(这样,如果您不从您的一个子视图中拖动,它将失败,这将允许您在其他地方工作的其他手势识别器......可能除非您使用多个手势识别器,否则这不是问题;它还将手势逻辑的血腥细节与视图控制器的其余部分隔离开来):

@interface PanBetweenSubviewsGestureRecognizer : UIPanGestureRecognizer
{
    NSMutableArray *_arrayOfFrames;
}

@property NSInteger startingIndex;
@property NSInteger endingIndex;

@end

@implementation PanBetweenSubviewsGestureRecognizer

@synthesize startingIndex = _startingIndex;
@synthesize endingIndex   = _endingIndex;

- (void)dealloc
{
    _arrayOfFrames = nil;
}

- (id)initWithTarget:(id)target action:(SEL)action
{
    self = [super initWithTarget:target action:action];
    if (self)
    {
        _arrayOfFrames = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addSubviewToArrayOfFrames:(UIView *)view
{
    [_arrayOfFrames addObject:[NSValue valueWithCGRect:view.frame]];
}

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

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    for (NSInteger i = 0; i < [_arrayOfFrames count]; i++)
    {
        if (CGRectContainsPoint([[_arrayOfFrames objectAtIndex:i] CGRectValue], location)) 
        {
            self.startingIndex = i;
            return;
        }
    }

    self.startingIndex = -1;
    self.endingIndex = -1;
    self.state = UIGestureRecognizerStateCancelled;
}

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

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    for (NSInteger i = 0; i < [_arrayOfFrames count]; i++)
    {
        if (CGRectContainsPoint([[_arrayOfFrames objectAtIndex:i] CGRectValue], location)) 
        {
            self.endingIndex = i;
            return;
        }
    }

    self.endingIndex = -1;
    self.state = UIGestureRecognizerStateCancelled;
}

@end

然后您可以按如下方式使用:

- (void)viewDidLoad
{
    [super viewDidLoad];

    PanBetweenSubviewsGestureRecognizer *pan = [[PanBetweenSubviewsGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [pan addSubviewToArrayOfFrames:self.view0];
    [pan addSubviewToArrayOfFrames:self.view1];
    [pan addSubviewToArrayOfFrames:self.view2];

    [self.view addGestureRecognizer:pan];

    // Do any additional setup after loading the view.
}

- (void)pan:(PanBetweenSubviewsGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded && sender.startingIndex >= 0 && sender.endingIndex >= 0 && sender.startingIndex != sender.endingIndex)
    {
        // successfully moved between subviews!
        NSLog(@"Moved from %1d to %1d", sender.startingIndex, sender.endingIndex);
    }
}
于 2012-07-12T03:15:52.647 回答