SKSpriteNode - 创建一个圆角节点?

Har*_*ain 31 iphone objective-c ios sprite-kit

有没有办法让SKSpriteNode圆形走投无路?我正在尝试用颜色填充的SKSpriteNode创建一个Tile likesqaure块:

SKSpriteNode *tile = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed:0.0/255.0
                                                                           green:128.0/255.0
                                                                            blue:255.0/255.0
                                                                           alpha:1.0] size:CGSizeMake(30, 30)];
Run Code Online (Sandbox Code Playgroud)

如何围绕走投无路?

谢谢!

Dob*_*pir 48

要获得圆角节点,您可以使用2种方法,每种方法都需要使用SKShapeNode.

第一种方法是使用SKShapeNode并将其路径设置为圆角矩形,如下所示:

SKShapeNode* tile = [SKShapeNode node];
[tile setPath:CGPathCreateWithRoundedRect(CGRectMake(-15, -15, 30, 30), 4, 4, nil)];
tile.strokeColor = tile.fillColor = [UIColor colorWithRed:0.0/255.0
                                                    green:128.0/255.0
                                                     blue:255.0/255.0
                                                    alpha:1.0];
Run Code Online (Sandbox Code Playgroud)

另一个使用精灵节点,裁剪节点和带圆角矩形的SKShapeNode作为裁剪节点掩码:

SKSpriteNode *tile = [SKSpriteNode spriteNodeWithColor:[UIColor   colorWithRed:0.0/255.0
                                                                           green:128.0/255.0
                                                                            blue:255.0/255.0
                                                                           alpha:1.0] size:CGSizeMake(30, 30)];
SKCropNode* cropNode = [SKCropNode node];
SKShapeNode* mask = [SKShapeNode node];
[mask setPath:CGPathCreateWithRoundedRect(CGRectMake(-15, -15, 30, 30), 4, 4, nil)];
[mask setFillColor:[SKColor whiteColor]];
[cropNode setMaskNode:mask];
[cropNode addChild:tile];
Run Code Online (Sandbox Code Playgroud)

如果你的瓷砖是一种纯色,我建议你采用第一种方法.

  • SKShapeNode现在有一个'rectOfSize:,cornerRadius:'初始值设定项 (9认同)

Zol*_*ler 16

在Swift 3中,您可以创建:

let tile = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 30, height: 30), cornerRadius: 10)
Run Code Online (Sandbox Code Playgroud)


小智 7

希望这可以帮助:

SKSpriteNode *make_rounded_rectangle(UIColor *color, CGSize size, float radius)
{
    UIGraphicsBeginImageContext(size);
    [color setFill];
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];
    [path fill];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    SKTexture *texture = [SKTexture textureWithImage:image];
    return [SKSpriteNode spriteNodeWithTexture:texture];
}
Run Code Online (Sandbox Code Playgroud)


Tho*_*yer 5

这很大程度上受到了公认答案的启发,但它使用了一种更易读的方式来创建SKShapeNode并修复了作物周围烦人的像素线。看似一个小细节,但可能会花费一些人几分钟的时间。

CGFloat cornerRadius = 15;
SKCropNode *cropNode = [SKCropNode node];
SKShapeNode *maskNode = [SKShapeNode shapeNodeWithRectOfSize:scoreNode.size cornerRadius:cornerRadius];
[maskNode setLineWidth:0.0];
[maskNode setFillColor:[UIColor whiteColor]];
[cropNode setMaskNode:maskNode];
[cropNode addChild:scoreNode];
Run Code Online (Sandbox Code Playgroud)


Fat*_*tie 5

原来如此简单啊......

class YourSprite: SKSpriteNode {

    func yourSetupFunction() {

          texture = SKTexture( image: UIImage(named: "cat")!.circleMasked! )
Run Code Online (Sandbox Code Playgroud)

没有更多的事情了。

你确实不能用SKShapeNode。

就是这么简单。使用 SKShapeNode 对性能造成了疯狂的影响,这不是正确的解决方案,它毫无意义地困难,而且 SKShapeNode 的目的与这个问题无关。

在此输入图像描述

看看他们都是小猫!

CircleMasked 的代码很简单:

(无论如何,所有处理图像的项目都需要这个。)

extension UIImage {
    
    var isPortrait:  Bool    { return size.height > size.width }
    var isLandscape: Bool    { return size.width > size.height }
    var breadth:     CGFloat { return min(size.width, size.height) }
    var breadthSize: CGSize  { return CGSize(width: breadth, height: breadth) }
    var breadthRect: CGRect  { return CGRect(origin: .zero, size: breadthSize) }
    
    var circleMasked: UIImage? {
        
        UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale)
        defer { UIGraphicsEndImageContext() }
        
        guard let cgImage = cgImage?.cropping(to: CGRect(origin:
            CGPoint(
                x: isLandscape ? floor((size.width - size.height) / 2) : 0,
                y: isPortrait  ? floor((size.height - size.width) / 2) : 0),
            size: breadthSize))
        else { return nil }
        
        UIBezierPath(ovalIn: breadthRect).addClip()
        UIImage(cgImage: cgImage, scale: 1, orientation: imageOrientation)
            .draw(in: breadthRect)
        return UIGraphicsGetImageFromCurrentImageContext()
    }

// classic 'circleMasked' stackoverflow fragment
// courtesy Leo Dabius /a/29047372/294884
}
Run Code Online (Sandbox Code Playgroud)

这里的所有都是它的。