-1

I'm trying to animate sprite resizing. Size changes, but without any animation.

Here's the problematic code:

func layout_scene() {
    backgroundColor = UIColor(red: 14/255, green: 145/255, blue: 137/255, alpha: 1.0);

    var char_trail: SKSpriteNode!
    char_trail = SKSpriteNode(imageNamed: "trail")
    char_trail.size = CGSize(width: frame.width / 8, height: frame.width / 12)
    char_trail.position = CGPoint(x: frame.minX + frame.width / 10,
                                  y: frame.minY + frame.height / 4.4)
    char_trail.zPosition = -0.1

    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(fire), userInfo: nil, repeats: true)
}


@objc func    fire()
{
    UIView.animate(withDuration: 1, animations: {
        self.char_trail.size.width += 10
    })
}

I expect char_trail size to change smoothly, but it just resizes in a moment. Tell me what I'm doing wrong, please

1 Answers1

0

To animate the size of a view you need to set view.transform instead of directly changing the width to the layer

view.transform = CGAffineTransformMakeScale(0,0);

0,0 refers to width,height and this code will only scale down the view until it disappear (not tested). If you want to scale it up till a fixed number you will first have to calculate the scale factor ((width + the_size_you_want_to_add) / width) -.... Keep in mind that if you dont scale the height in the same factor the view will stretch.

Noel Carcases
  • 711
  • 1
  • 7
  • 27