我有一个父 View 和 3 个独立的 subview 。 subview 在父 View 中展开,没有重叠(并且之间有一些空间)。当用户在屏幕上移动手指(不抬起手指)时,我想在进入和退出每个 subview 时跟踪触摸。
示例:如果用户开始触摸屏幕上 subview 之外的某个位置,然后将手指滑过 subview 1、 subview 1、 subview 2,然后放开,我期望这些事件被触发:
- 触摸开始
- 触摸输入的子项 1
- 触摸已退出的子级 1
- 触摸输入的子项 2
- 触摸结束
在这种情况下,touchesBegan:withEvent:和touchesEnded:withEvent:方法似乎会很有帮助,但是当我在 subview Controller 上定义它们时,它们并没有完全按照我想要的方式执行——如果用户开始触摸 subview 外部,然后在 subview 上滑动, subview 本身不会触发触摸事件。
当前解决方案:我目前正在使用一种解决方案来完成此任务,感觉非常棘手。我正在观察父级上的touchesBegan:withEvent:、touchesEnded:withEvent: 和touchesMoved:withEvent:,获取每个事件的坐标,并确定它们是否位于子级的范围内。如果他们这样做,我会触发如上所述的适当事件。
这种方法大部分有效,但感觉效率很低。感觉框架应该为我处理这项工作。我的状态管理代码有时也会错过“进入”或“退出”触发器,我怀疑这是因为触摸事件要么被丢弃,要么以意外的顺序出现。我在这里缺少更好的方法吗?
最佳答案
最简单的解决方案是这样的:
- (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);
}
}
}
也许更优雅的是定义您自己的自定义手势识别器(这样,如果您不从 subview 之一拖动,它将失败,这将允许您可能在其他地方工作的其他手势识别器。 .. 除非您使用多个手势识别器,否则可能不是问题;它还将手势逻辑的血腥细节与 View Controller 的其余部分隔离开来):
@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);
}
}
关于objective-c - 观察一次 Action 对多个 View 的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11443213/