栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Swift Spritekit以编程方式添加按钮

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swift Spritekit以编程方式添加按钮

在SpriteKit中添加按钮并响应其点击并不像在UIKit中那样容易。基本上,您需要创建

SKNode
某种类型的对象,以绘制您的按钮,然后检查场景中记录的触摸是否在该节点的范围内。

一个非常简单的场景,中心只有一个红色矩形作为按钮,看起来像这样:

class ButtonTestScene: SKScene {    var button: SKNode! = nil    override func didMoveToView(view: SKView) {        // Create a simple red rectangle that's 100x44        button = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))        // Put it in the center of the scene        button.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));        self.addChild(button)    }    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {        // Loop over all the touches in this event        for touch: AnyObject in touches { // Get the location of the touch in this scene let location = touch.locationInNode(self) // Check if the location of the touch is within the button's bounds if button.containsPoint(location) {     println("tapped!") }        }    }}

如果您需要一个外观和动画效果类似于UIKit中的按钮,则需要自己实现;SpriteKit没有内置任何内容。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660407.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号