Resize sprite without reducing content

I created a big circle with UIBezierPath and turned it into Sprite using this,

let path = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0), radius: CGFloat(226), startAngle: 0.0, endAngle: CGFloat(M_PI * 2), clockwise: false)

// create a shape from the path and customize it
let shape = SKShapeNode(path: path.cgPath)
shape.lineWidth = 20
shape.position = center
shape.strokeColor = UIColor(red:0.98, green:0.99, blue:0.99, alpha:1.00)

let trackViewTexture = self.view!.texture(from: shape, crop: outerPath.bounds)
let trackViewSprite = SKSpriteNode(texture: trackViewTexture)
trackViewSprite.physicsBody = SKPhysicsBody(edgeChainFrom: innerPath.cgPath)
self.addChild(trackViewSprite)

It works great. He creates a circle perfectly. But I need to resize it using

SKAction.resize(byWidth: -43, height: -43, duration: 0.3)

This will make it a little smaller. But when resizing 20 the line width that I set is now very small due to the filling aspect. So when I shade it looks something like this:

enter image description here

But I need it to shrink like this: keeping the line width 20:

enter image description here

How should I do it?

I don’t know if this will affect anything, but sprites rotate with SKAction forever

- EDIT - Now, how can I use this method to scale to a specific size? How to rotate 226x226 to 183x183?

+1
1

, , , , , . , 2 .

:

  • run(SKAction, completion: @escaping () -> Void). , , , , . , .

  • , lineWidth. SKAction customAction. :

    let scale = CGFloat(0.5)
    let finalLineWidth = initialLineWidth / scale
    let animationDuration = 1.0
    
    let scaleAction = SKAction.scale(by: scale, duration: animationDuration)
    
    let lineWidthAction = SKAction.customAction(withDuration: animationDuration) { (shapeNode, time) in
        if let shape = shapeNode as? SKShapeNode {
            let progress = time / CGFloat(animationDuration)
    
            shape.lineWidth = initialLineWidth + progress * (finalLineWidth - initialLineWidth)
        }
    }
    
    let group = SKAction.group([scaleAction, lineWidthAction])
    
    shape.run(group)
    

0,5, 10, 20. scaleAction , , , actionBlock, , , . , , run.

Bezier , init(circleOfRadius: CGFloat) SKShapeNode, .

+4

Source: https://habr.com/ru/post/1657756/


All Articles