user3305606
user3305606

Reputation: 33

Set a button as an image in SpriteKit

I currently have a UIButton as a restart button for a game within SpriteKit and I'd like to make it an image. I have declared the button as:

var RateBtn: UIButton! 

Here's current code for button:

RestartBtn = UIButton(frame: CGRect(x: 0, y: 0, width: view!.frame.size.width / 3, height: view!.frame.size.height / 3))
RestartBtn.center = CGPointMake(view!.frame.size.width / 2, view!.frame.size.height)
RestartBtn.setTitle("RESTART", forState: UIControlState.Normal)
RestartBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

RestartBtn.addTarget(self, action: Selector("restart"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(RestartBtn)

UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: nil, animations: ({
    self.RestartBtn.center.y = self.view!.frame.size.height / 1.4
}), completion: nil)

So I have it set out with a spring effect but I now want it as an image instead of font. How do I do this?

Upvotes: 0

Views: 127

Answers (1)

Aryaman Goel
Aryaman Goel

Reputation: 538

Make the button a SKSpriteNode. Detect touch on it by:

for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if button.containsPoint(location) {

             //enter restart function here!

        }
    }

Upvotes: 1

Related Questions