【问题标题】:SpriteKit Rendering Issue in iOS 8 for drawing appiOS 8 中用于绘图应用的 SpriteKit 渲染问题
【发布时间】:2014-10-20 17:50:08
【问题描述】:

我有一个绘图模拟 SKScene,它在 iOS 7 中运行良好,但在 iOS 8 中无法运行。这既适用于模拟器,也适用于设备。

场景应该在手指触摸屏幕的地方显示黑线,并且在您完成“绘制”一条线后它们应该持续存在。这是它在 iOS 7 中的屏幕截图:

虽然没有崩溃,但在 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 中找到的代码编写的。

【问题讨论】:

    标签: ios8 sprite-kit xcode6


    【解决方案1】:

    我在使用 Sprite Kit iOS8 游戏时遇到了类似的问题,并通过以下方式对其进行了修复:尝试在 touchesMoved 函数中的 CGPathAddLineToPoint 调用之前立即添加一个 CGPathMoveToPoint 调用。

    根据 CGPathAddLineToPoint 的类引用,调用 CGPathAddLineToPoint 会自动“将当前点更新到指定位置 (x,y) [新端点]”。但是,直到我在每次 CGPathAddLineToPoint 调用之前通过调用 CGPathMoveToPoint 手动执行此操作,我的线条才在 iOS8 中正确呈现。不知道为什么会这样。可能是 iOS8 中 Sprite Kit 的一个错误。

    请在下面找到修改后的代码,其中的更改标有/* 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多