在我目前的项目中,我正在使用UITapGestureRecognizer。我编写了它,以便当点击屏幕的左侧时,一个物体移动,当点击屏幕的右侧时,另一个物体移动。但是,手势识别器仅在轻敲右侧时才起作用。另外,我想这样做,如果双方同时被轻敲,可以执行SKActions。我不明白我做错了什么。我的代码如下。提前致谢
在ViewDidLoad
中UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
[tap1 setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap1];
view.multipleTouchEnabled = YES;
-(void)tap1:(UIGestureRecognizer *)gestureRecognizer {
SKNode *person11 = [self childNodeWithName:@"person1"];
SKNode *person2 = [self childNodeWithName:@"person2"];
CGPoint pt = [gestureRecognizer locationInView:self.view];
if (pt.x > (self.view.bounds.size.width/2))
{
if (person2.position.x == CGRectGetMidX(self.frame) + 400 ) {
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveLeft2];
}
if (person2.position.x == CGRectGetMidX(self.frame) + 90 ) {
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveRight2];
}
}
if (pt.x < (self.view.bounds.size.width/2)){
if (person11.position.x == CGRectGetMidX(self.frame) - 90 ) {
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person11 runAction:moveLeft2];
}
if (person11.position.x == CGRectGetMidX(self.frame) - 400 ) {
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person11 runAction:moveRight2];
}
}
}
答案 0 :(得分:1)
而不是手势识别器,在touchesEnded中完成工作。这是一个例子:
<强>夫特强>
override func viewDidLoad() {
super.viewDidLoad()
self.view.multipleTouchEnabled = true
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
if(touch.locationInView(self.view).x<self.view.frame.midX) {
print("On the left half") // run the respective SKAction
} else {
print("On the right half") // run the respective SKAction
}
}
}
目标C (应该可以使用,但我使用Swift并在手机上翻译,所以这可能无法完美运行)
-(void)viewDidLoad {
self.view.multipleTouchEnabled = YES
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) {
if(touch.locationInView(self.view).x<self.view.frame.midX) {
printf("On the left half\n"); // run the respective SKAction
} else {
printf("On the right half\n"); // run the respective SKAction
}
}
}
这将完全符合您的要求。
非常自我解释:允许多次触摸,然后,当触摸被释放时,它会检查视图中的相对水平位置(.x
),并查看它是否位于视图的左半部分或右半部分。不需要手势识别器!
我稍后会尝试仔细检查Objective C代码,但Swift绝对有用。
如果您有任何疑问,请告诉我!
答案 1 :(得分:0)
尝试把它(在Swift中):
Reload JS
很抱歉我经常使用Swift ...我猜你也可以在Objective-C上找到一个内置的gestureRecognizer()。