[Swift] 利用accessibilityIdentifier辨別view來實現閱讀條文後才能勾選我同意的功能
swift
當我們在同一個view中,需要判斷兩個scrollView是否已經滑到底部時,我們可以利用accessibilityIdentifier這個屬性來為每個scrollView作標記,並且在delegate中判斷是哪個scrollerView已經滑到底部,再讓相對應的button變為可勾選。
@IBOutlet weak var firstTextView: UITextView!
@IBOutlet weak var agreeFirstTextViewBtn: UIButton!
@IBOutlet weak var secondTextView: UITextView!
@IBOutlet weak var agreeSecondTextViewBtn: UIButton!
設定各個view的accessibilityIdentifier,delegate,讓對應的button先設為不能點選
override func viewDidLoad() {
super.viewDidLoad()
firstTextView.accessibilityIdentifier = "firstTextView"
secondTextView.accessibilityIdentifier = "secondTextView"
firstTextView.delegate = self
secondTextView.delegate = self
agreeFirstTextViewBtn.isEnabled = false
agreeSecondTextViewBtn.isEnabled = false
}
這邊是先判斷scrollView是否已經滑到最底部,再用switch case判斷是哪個scrollerView,再讓相對應的button改變為可以點選。
因為我們的條文是用UITextView去製作,所以這邊實現的是UITextViewDelegate。
extension ViewController: UITextViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let height = scrollView.frame.size.height
let contentOffsetY = scrollView.contentOffset.y
let bottomOffset = scrollView.contentSize.height - contentOffsetY
if bottomOffset <= height {
switch scrollView.accessibilityIdentifier {
case "firstTextView":
self.agreeFirstTextViewBtn.isEnabled = true
case "secondTextView":
self.agreeSecondTextViewBtn.isEnabled = true
default:
self.agreeFirstTextViewBtn.isEnabled = false
self.agreeSecondTextViewBtn.isEnabled = false
}
}
}
}