【发布时间】:2018-04-17 12:39:43
【问题描述】:
我正在尝试继承 UIView 并设计一个透明视图。此视图将位于许多其他视图之上,它的唯一任务是捕获和记录用户触摸(点击和平移)。我尝试了许多不同的方法,并在其他用户提出的不同问题中进行了解释,但没有运气。这是我迄今为止在我的实现文件中所做的:
#import "touchLayer.h"
@implementation touchLayer
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) [self commonInit];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) [self commonInit];
return self;
}
- (void)commonInit
{
self.userInteractionEnabled = YES;
self.alpha = 0.0;
}
- (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id hitView = [super hitTest:point withEvent:event];
if (hitView == self)
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.phase == UITouchPhaseBegan) NSLog(@"touchesBegan");
else if (touch.phase == UITouchPhaseMoved) NSLog(@"touchesMoved");
else if (touch.phase == UITouchPhaseEnded) NSLog(@"touchesEnded");
return nil;
}
else
return hitView;
}
@end
现在这段代码工作得很好,我看到了较低层的触摸,但我无法区分 touchBegan、touchMoved 和 touchEnded。 [[event allTouches] anyObject] 返回 nil。您知道如何在不阻止触摸的情况下捕获 UIView 上的点击和平移吗?非常感谢。
【问题讨论】:
-
所以现在,你的问题是这个视图阻止了下面视图中的触摸。对吗?
-
没有实际的触摸被传递到下视图。但是,我需要知道这个 UIView 捕获了什么样的触摸(开始、移动、结束),但是 event.allTouches.anyObject 返回 nil。
-
据我所知,我确信透明视图不会收到触摸。尝试给它一个颜色,你会看到不同
-
我也尝试过使用颜色和 alpha 通道。它确实收到了触摸,但就像我说的我无法获得 UITouchPhase。是零。
-
尝试使用
func touchesBegan(touches: NSSet, withEvent event: UIEvent)、func touchesMoved(touches: NSSet, withEvent event: UIEvent)和touchesEnd这3种方法
标签: ios uiview uigesturerecognizer uitouch uievent