在一个动作中观察多个视图的触摸

时间:2012-07-11 23:48:12

标签: objective-c ios cocoa-touch events touch

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

示例:如果用户开始触摸子视图之外的屏幕上的某个位置,则将手指滑过1号儿童,1号儿童,2号儿童,然后放开,I会期望触发这些事件:

  1. 触摸开始
  2. 触摸输入的孩子1
  3. 触及退出的孩子1
  4. 触摸输入的孩子2
  5. 触控结束
  6. 似乎touchesBegan:withEvent:和touchesEnded:withEvent:方法在这种情况下会有所帮助,但是当我在子视图控制器上定义它们时,它们并没有完全符合我的要求 - 如果用户开始触摸子视图外,然后在子视图上滑动,不会触发孩子本身的触摸事件。

    当前解决方案:我目前正在使用一种非常难以实现此目标的解决方案。我正在观察touchesBegan:withEvent:,touchesEnded:withEvent:和touchesMoved:withEvent:在父节点上,抓住每个事件的坐标,并确定它们是否位于孩子的范围内。如果他们这样做,我会触发上述相应的事件。

    这种方法大多有效,但感觉非常低效。感觉框架应该为我处理这项工作。我的州管理代码有时也会错过“输入”或“退出”触发器,我怀疑这是因为触摸事件要么被丢弃,要么以意想不到的顺序来找我。我在这里错过了一个更好的方法吗?

1 个答案:

答案 0 :(得分: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);
    }
}