现在我在我的SKScene中覆盖了accessibilityElementAtIndex,accessibilityElementCount和indexOfAccessibilityElement.
他们返回一组可访问的元素:
func loadAccessibleElements(){ self.isAccessibilityElement = false let pIEces = getAllPIEces() accessibleElements.removeAll(keepCapacity: false) for pIEce in pIEces { let element = UIAccessibilityElement(accessibilityContainer: self.usableVIEw!) element.accessibilityFrame = pIEce.getAccessibilityFrame() element.accessibilityLabel = pIEce.getText() element.accessibilityTraits = UIAccessibilityTraitbutton accessibleElements.append(element) }}
其中pIEce是SKSpriteNode的子类,并定义了getAccessibilityFrame:
func getAccessibilityFrame() -> CGRect{ return parentVIEw!.convertRect(frame,toVIEw: nil)}
现在,一个(尺寸错误的)辅助功能元素似乎出现在屏幕上错误的地方.
有人能指出我正确的方向吗?
非常感谢
编辑:
我已经尝试了一个Hack-ish工作,将UIVIEw放在SKVIEw上,UIbutton元素放在与SKSpriteNodes相同的位置.但是,可访问性仍然不希望工作.视图按如下方式加载:
func loadAccessibilityVIEw(){ vIEw.isAccessibilityElement = false vIEw.accessibilityElementsHIDden = false skVIEw.accessibilityElementsHIDden = false let accessibleSubvIEw = UIVIEw(frame: vIEw.frame) accessibleSubvIEw.userInteractionEnabled = true accessibleSubvIEw.isAccessibilityElement = false vIEw.addSubvIEw(accessibleSubvIEw) vIEw.bringSubvIEwToFront(accessibleSubvIEw) let pIEces = (skVIEw.scene! as! GameScene).getAllPIEces() for pIEce in pIEces { let pIEcebutton = UIbutton(frame: pIEce.getAccessibilityFrame()) pIEcebutton.isAccessibilityElement = true pIEcebutton.accessibilityElementsHIDden = false pIEcebutton.accessibilityTraits = UIAccessibilityTraitbutton pIEcebutton.setTitle(pIEce.getText(),forState: UIControlState.normal) pIEcebutton.setBackgroundImage(UIImage(named: "blue-button"),forState: UIControlState.normal) pIEcebutton.Alpha = 0.2 pIEcebutton.accessibilityLabel = pIEce.getText() pIEcebutton.accessibilityFrame = pIEcebutton.frame pIEcebutton.addTarget(self,action: Selector("dIDTap:"),forControlEvents: UIControlEvents.touchUpInsIDe) accessibleSubvIEw.addSubvIEw(pIEcebutton) } UIAccessibilityPostNotification(UIAccessibilityScreenChangednotification,nil)}
按钮放置正确,但可访问性根本无法正常工作.似乎有些东西阻止它起作用.
解决方法 我搜索到如何使用SpriteKit在Swift中实现VoiceOver的描述是徒劳的,所以我终于想出了如何做到这一点.这里有一些工作代码,可以在添加到SKScene类时将SKNode转换为可访问的按钮:// Add the following code to a scene where you want to make the SKNode variable named “leave” an accessible button// leave must already be initialized and added as a child of the scene,or a child of other SKNodes in the scene// screenHeight must already be defined as the height of the device screen,in points// Accessibilityprivate var accessibleElements: [UIAccessibilityElement] = []private func nodetoDevicePointsFrame(node: SKNode) -> CGRect { // first convert from frame in SKNode to frame in SKScene's coordinates var sceneFrame = node.frame sceneFrame.origin = node.scene!.convertPoint(node.frame.origin,fromNode: node.parent!) // convert frame from SKScene coordinates to device points // sprite kit scene origin is in lower left,accessibility device screen origin is at upper left // assumes scene is initialized using SKScenescaleMode.Fill using dimensions same as device points var deviceFrame = sceneFrame deviceFrame.origin.y = CGfloat(screenHeight-1) - (sceneFrame.origin.y + sceneFrame.size.height) return deviceFrame}private func initaccessibility() { if accessibleElements.count == 0 { let accessibleLeave = UIAccessibilityElement(accessibilityContainer: self.vIEw!) accessibleLeave.accessibilityFrame = nodetoDevicePointsFrame(leave) accessibleLeave.accessibilityTraits = UIAccessibilityTraitbutton accessibleLeave.accessibilityLabel = “leave” // the accessible name of the button accessibleElements.append(accessibleLeave) }}overrIDe func dIDMovetoVIEw(vIEw: SKVIEw) { self.isAccessibilityElement = false leave.isAccessibilityElement = true}overrIDe func willMoveFromVIEw(vIEw: SKVIEw) { accessibleElements = []}overrIDe func accessibilityElementCount() -> Int { initaccessibility() return accessibleElements.count}overrIDe func accessibilityElementAtIndex(index: Int) -> AnyObject? { initaccessibility() if (index < accessibleElements.count) { return accessibleElements[index] as AnyObject } else { return nil }}overrIDe func indexOfAccessibilityElement(element: AnyObject) -> Int { initaccessibility() return accessibleElements.indexOf(element as! UIAccessibilityElement)!}总结
以上是内存溢出为你收集整理的swift – 使用Sprite Kit的辅助功能(Voice Over)全部内容,希望文章能够帮你解决swift – 使用Sprite Kit的辅助功能(Voice Over)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)