Can I add a border to an SKSpriteNode, similar to UIView?

泄露秘密 提交于 2019-12-23 07:42:13

问题


I'm interested if an SKSpriteNode can be made to imitate the behavior of a UIView where I can specify border and corner radius?

self.view.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.view.layer.borderWidth = 2;
self.view.layer.cornerRadius = 2;
self.view.layer.masksToBounds = YES;

回答1:


Not natively. Though you can just add a SKShapeNode as child whose path you create with CGPathCreateWithRoundedRect.




回答2:


The following is a simple, dropin extension for SKSpriteNode that will allow you to draw a border with a given color around your node.

import SpriteKit

extension SKSpriteNode {
    func drawBorder(color: UIColor, width: CGFloat) {
        let shapeNode = SKShapeNode(rect: frame)
        shapeNode.fillColor = .clear
        shapeNode.strokeColor = color
        shapeNode.lineWidth = width
        addChild(shapeNode)
    }
}


来源:https://stackoverflow.com/questions/20889222/can-i-add-a-border-to-an-skspritenode-similar-to-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!