2014-10-20 78 views
1

我有一个绘图模拟SKScene,在iOS 8中无法正常工作,这在iOS 8中不起作用。这既适用于模拟器,也适用于设备。SpriteKit在iOS 8中绘制应用程序的渲染问题

场景应显示手指触摸屏幕的黑色线条,并且在完成“绘制”线条后,它们应该保留。下面是它在iOS的7截图:

enter image description here

虽然没有崩溃,行不呈现在所有iOS中8.我刚得到一个空白的画布。 NSLogging表示它确实注册了touchesBegan/Moved/Ended函数。

我已经产生整个类将其全部:

@implementation CSDraw 

-(id)initWithSize:(CGSize)size type:(NSString *)CSType stresslevel:(NSInteger)stress_indicator { //designated initializer 
    if (self = [super initWithSize:size type: CSType stresslevel:stress_indicator]) { 
     NSLog(@"Creating new scene from CSDraw within the init"); 
    } 
    return self; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    self.swiped = NO; 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 

    self.pathToDraw = CGPathCreateMutable(); 

    CGPathMoveToPoint(self.pathToDraw, NULL, positionInScene.x, positionInScene.y); 

    self.lineNode = [SKShapeNode node]; 
    self.lineNode.path = self.pathToDraw; 
    self.lineNode.strokeColor = [SKColor blackColor]; 
    self.lineNode.lineWidth = 10; 
    self.lineNode.zPosition = 50; 

    [self addChild:self.lineNode]; 

} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.swiped = YES; 
    UITouch* touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 

    CGPathAddLineToPoint(self.pathToDraw, NULL, positionInScene.x, positionInScene.y); 
    self.lineNode.path = self.pathToDraw; 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch* touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 

    if(!self.swiped) { //user just tapped once, draw a single point 
     SKSpriteNode *dot = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(10, 10)]; 
     dot.position = positionInScene; 
     [self addChild: dot]; 

    } else { //calls touchesMoved 
    } 

    //[self.lineNode removeFromParent]; //comment out this line if you want line to remain on screen 
    CGPathRelease(self.pathToDraw); 
} 
@end 

这个类是从我在this StackOverFlow answer找到的代码写入。

回答

0

我有一个Sprite Kit iOS8游戏的类似问题,并用类似下面的方法修复它:尝试在touchesMoved函数中的CGPathAddLineToPoint调用之前立即添加CGPathMoveToPoint调用。

根据CGPathAddLineToPoint的类引用,自动调用CGPathAddLineToPoint“将当前点更新到指定位置(x,y)[新端点]”。但是,我的行在iOS8中没有正确呈现,除非我在每次调用CGPathAddLineToPoint之前通过调用CGPathMoveToPoint手动完成此操作。不知道这是为什么。也许是iOS8中的Sprite Kit的一个bug。

请在下方找到修改后的代码,其中标有/* new */评论的更改。该代码假定您有一个名为lastPointTouched的CGPoint属性。

@implementation CSDraw 

-(id)initWithSize:(CGSize)size type:(NSString *)CSType stresslevel:(NSInteger)stress_indicator { //designated initializer 
    if (self = [super initWithSize:size type: CSType stresslevel:stress_indicator]) { 
     NSLog(@"Creating new scene from CSDraw within the init"); 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    self.swiped = NO; 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 

    self.pathToDraw = CGPathCreateMutable(); 

    CGPathMoveToPoint(self.pathToDraw, NULL, positionInScene.x, positionInScene.y); 
    /* new */ self.lastPointTouched = positionInScene; 

    self.lineNode = [SKShapeNode node]; 
    self.lineNode.path = self.pathToDraw; 
    self.lineNode.strokeColor = [SKColor blackColor]; 
    self.lineNode.lineWidth = 10; 
    self.lineNode.zPosition = 50; 

    [self addChild:self.lineNode]; 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.swiped = YES; 
    UITouch* touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 

    /* new */ CGPathMoveToPoint(self.pathToDraw, NULL, self.lastPointTouched.x, self.lastPointTouched.y); 
    CGPathAddLineToPoint(self.pathToDraw, NULL, positionInScene.x, positionInScene.y); 
    /* new */ self.lastPointTouched = positionInScene; 
    self.lineNode.path = self.pathToDraw; 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch* touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 

    if(!self.swiped) { //user just tapped once, draw a single point 
     SKSpriteNode *dot = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(10, 10)]; 
     dot.position = positionInScene; 
     [self addChild: dot]; 

    } else { //calls touchesMoved 
    } 

    //[self.lineNode removeFromParent]; //comment out this line if you want line to remain on screen 
    CGPathRelease(self.pathToDraw); 
} 
@end