在SpriteKit中将按钮设置为图像

时间:2015-04-29 14:56:04

标签: ios swift uibutton sprite-kit

我目前在SpriteKit中有一个UIButton作为游戏的重启按钮,我希望将其设为图像。我已将按钮声明为:

var RateBtn: UIButton! 

这是按钮的当前代码:

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)

所以我让它以弹簧效果开始,但我现在想要它作为图像而不是字体。我该怎么做?

1 个答案:

答案 0 :(得分:1)

将按钮设为SKSpriteNode。 通过以下方式检测触摸:

for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if button.containsPoint(location) {

             //enter restart function here!

        }
    }