2014-10-20 82 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