UIAccessibilityTrait adjustable does not enter increment() or decrement() swift

拈花ヽ惹草 提交于 2019-12-11 12:53:47

问题


I have an element (an imageView) which shows a number. I want the voice over user to swipe up to increment and swipe down to decrement. There are a total of six numbers (images) and each number shoes different content on screen.

For that intent I made the view adjustable:

imageView.accessibilityTraits = .adjustable

I also implemented the following methods:

override func accessibilityIncrement() {
    print("increment")
}

override func accessibilityDecrement() {
    print("decrement")
}

However, when I test this, Xcode won't go inside the increment or decrement methods.

What am I making wrong? Is there maybe a better way to increment or decrement a value using voice over?


回答1:


I guess you implemented the accessibilityIncrement() and accessibilityDecrement() methods in your view controller but they should belong to the created image view whose trait should be .adjustable.

Just subclass UIImageView as follows in this basic example:

class MyImageView: UIImageView {

    override var isAccessibilityElement: Bool {
        get { return true }
        set { }
    }

    override var accessibilityTraits: UIAccessibilityTraits {
        get { return .adjustable }
        set { }
    }

    override func accessibilityIncrement() {
        print("INCREMENT")
    }

    override func accessibilityDecrement() {
        print("DECREMENT")
    }
}

You just have to define what to do in each adjustable method and that should do the job.

I suggest you take a look at this site where a complete example about adjustable values with code snippets and illustrations is provided for both ObjC and Swift.



来源:https://stackoverflow.com/questions/56628631/uiaccessibilitytrait-adjustable-does-not-enter-increment-or-decrement-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!