UITapGestureRecognizer 无法正常执行
UITapGestureRecognizer won't execute properly
在我当前的项目中,我正在使用 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];
}
}
}
试着把这个(在Swift中):
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
抱歉,我经常使用 Swift...我想您也可以在 Objective-C 上找到一个内置的 gestureRecognizer()。
代替手势识别器,在 touchesEnded 中完成工作。这是一个例子:
Swift
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
}
}
}
Objective C(应该可以,但我使用 Swift 并在我的 phone 上翻译,所以这可能无法完美工作)
-(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 绝对有效。
如有任何问题,请告诉我!
在我当前的项目中,我正在使用 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];
}
}
}
试着把这个(在Swift中):
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
抱歉,我经常使用 Swift...我想您也可以在 Objective-C 上找到一个内置的 gestureRecognizer()。
代替手势识别器,在 touchesEnded 中完成工作。这是一个例子:
Swift
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
}
}
}
Objective C(应该可以,但我使用 Swift 并在我的 phone 上翻译,所以这可能无法完美工作)
-(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 绝对有效。
如有任何问题,请告诉我!