我正在尝试在SKScene中创建一个UIRotationGestureRecognizer,并将其添加到场景视图中。当我将手势添加到视图时,我将其目标指定给场景,并将动作指定给场景中的方法。场景的方法永远不会被调用,手势识别器的视图属性始终为NULL。
//
// MyScene.h
// Rotate SpaceShip
//
// Copyright (c) 2014 Andrew Paterson. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
@interface MyScene : SKScene <UIGestureRecognizerDelegate>
@end
实施:
//
// MyScene.m
// Rotate SpaceShip
//
// Created by Andrew Paterson on 2/23/14.
// Copyright (c) 2014 Andrew Paterson. All rights reserved.
//
#import "MyScene.h"
@interface MyScene ()
@property (strong, nonatomic)UIRotationGestureRecognizer *rotationGestureRecognizer;
@end
@implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKSpriteNode *spaceShip = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
spaceShip.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
spaceShip.name = @"spaceship";
[self addChild:spaceShip];
self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotation:)];
self.rotationGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:self.rotationGestureRecognizer];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
[super touchesBegan:touches withEvent:event];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
- (void)moveFromScene{
[self.view removeGestureRecognizer:self.rotationGestureRecognizer];
}
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer{
SKSpriteNode *spaceship = (SKSpriteNode *)[self childNodeWithName:@"spaceship"];
if (!(recognizer.state == UIGestureRecognizerStateEnded)){
spaceship.zRotation = (spaceship.zRotation + (recognizer.rotation - spaceship.zRotation));
}
}
@end
当我在更新时中断并打印手势识别器
时,调试器输出此信息(lldb) po self.rotationGestureRecognizer.view
nil
(lldb) po self.rotationGestureRecognizer
<UIRotationGestureRecognizer: 0xa04d570; state = Possible; view = <(null) 0x0>; target= <(action=handleRotation:, target=<MyScene 0x9632380>)>>
(lldb)
非常感谢任何帮助解决这个非常奇怪的问题。
答案 0 :(得分:0)
显然,在initWithSize中添加手势识别器时,sprite工具包中存在一个导致此问题的错误:如果将其移动到didMoveToView:问题就会消失。