Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Sprite Kit Node using Pinch Gesture

I'm trying to implement pinch gesture recognizer to resize my sprite node. I'm using setScale(sender.scale) to do that, but every time I lift my fingers and try to pinch again, my sprite reset to 1.0 scale before scaling to the pinch again.

What I want is when I re-pinch the screen, the sprite size stays as it is, and it grow bigger as I pinch out or smaller as I pinch in, so I can keep pinching to make it as big or as small as it can. How to do this?

Here's my code.

var pizza = PizzaSprite()

override func didMove(to view: SKView) {

    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinchFrom(_:)))

    pizza = PizzaSprite(size: self.frame.width * 0.25)

    self.addChild(pizza)

    self.view?.addGestureRecognizer(pinchGesture)

}

func handlePinchFrom(_ sender: UIPinchGestureRecognizer) {

    if sender.state == .began {

    } else if sender.state == .changed {

        pizza.setScale(sender.scale)

    } else if sender.state == .ended {

    }

}

Here's the PizzaSprite class

init() {
    let texture = SKTexture(imageNamed: "demPizza")
    super.init(texture: texture, color: UIColor.clear, size: texture.size())
}

init(size: CGFloat) {
    let texture = SKTexture(imageNamed: "demPizza")
    super.init(texture: texture, color: UIColor.clear, size: CGSize(width: size, height: size))
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
like image 588
Fakhrian B Avatar asked Jan 04 '17 07:01

Fakhrian B


1 Answers

So, I've been working at this for a week now, been given up once and use only pan instead, but I had to retry using pinch. Just some minutes after I posted this question, I got the answer myself....

I'm posting here in case someone need this.

func handlePinchFrom(_ sender: UIPinchGestureRecognizer) {

    let pinch = SKAction.scale(by: sender.scale, duration: 0.0)

    pizza.run(pinch)
    sender.scale = 1.0

}
like image 85
Fakhrian B Avatar answered Nov 11 '22 20:11

Fakhrian B