如何只允许在场景中的特定区域进行触摸?
How to allow touching only in specific area in the scene?
我想将触摸设置为仅在屏幕的一部分我尝试在不允许触摸的屏幕部分添加节点层并禁用用户交互
_mainLayer.userInteractionEnabled = NO;
但是没有用,不知道该怎么做?
我没有足够的评论空间来深入探讨,所以这只是一个准答案。
在评论中的屏幕截图中,您似乎不希望在视图底部 200 像素高的区域中的任何地方识别触摸。
您可以让您的视图采用 UIGestureRecognizerDelegate 协议并实现 shouldReceiveTouch 方法。尝试像这样实现该方法(我实际上有一段时间没有使用 Objective-C 所以如果任何语法完全关闭请原谅我):
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint touchPointInView = [touch locationInView:self];
if (touchPointInView.y >= CGRectGetMaxY(self.bounds) - 200)
{
return NO;
}
else
{
return YES;
}
}
不要忘记设置手势识别器的委托(在视图的构造函数中往往是个好地方):
gestureRecognizer.delegate = self;
以下是如何将触摸事件限制在特定区域的示例:
Swift
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
switch (location) {
case let point where CGRectContainsPoint(rect, point):
// Touch is inside of a rect
...
case let point where CGPathContainsPoint(path, nil, point, false):
// Touch is inside of an arbitrary shape
...
default:
// Ignore all other touches
break
}
}
}
Obj-C
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rect, location)) {
}
else if (CGPathContainsPoint(path, nil, location, false)) {
}
}
}
我想将触摸设置为仅在屏幕的一部分我尝试在不允许触摸的屏幕部分添加节点层并禁用用户交互
_mainLayer.userInteractionEnabled = NO;
但是没有用,不知道该怎么做?
我没有足够的评论空间来深入探讨,所以这只是一个准答案。
在评论中的屏幕截图中,您似乎不希望在视图底部 200 像素高的区域中的任何地方识别触摸。
您可以让您的视图采用 UIGestureRecognizerDelegate 协议并实现 shouldReceiveTouch 方法。尝试像这样实现该方法(我实际上有一段时间没有使用 Objective-C 所以如果任何语法完全关闭请原谅我):
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint touchPointInView = [touch locationInView:self];
if (touchPointInView.y >= CGRectGetMaxY(self.bounds) - 200)
{
return NO;
}
else
{
return YES;
}
}
不要忘记设置手势识别器的委托(在视图的构造函数中往往是个好地方):
gestureRecognizer.delegate = self;
以下是如何将触摸事件限制在特定区域的示例:
Swift
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
switch (location) {
case let point where CGRectContainsPoint(rect, point):
// Touch is inside of a rect
...
case let point where CGPathContainsPoint(path, nil, point, false):
// Touch is inside of an arbitrary shape
...
default:
// Ignore all other touches
break
}
}
}
Obj-C
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rect, location)) {
}
else if (CGPathContainsPoint(path, nil, location, false)) {
}
}
}