问题
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