ScrollView ไม่มี delegate ให้ทิศทาง scroll โดยตรง ใช้ UISwipeGestureRecognizer แทน
ขั้นตอนใน Storyboard
- ลาก Swipe Gesture Recognizer วางเหนือ Scroll View
- Attribute Inspector → Swipe → ตั้งค่า Direction: Up (หรือ Down)
- สร้าง IBAction เชื่อมกับ gesture recognizer
โค้ดจัดการ Swipe
@IBAction func swipeMade(_ sender: UISwipeGestureRecognizer) {
if sender.direction == .up {
print("up swipe made")
// ทำงานที่ต้องการที่นี่
}
}
ผลลัพธ์
เมื่อสวายップบน Scroll View จะพิมพ์ "up swipe made" ใน console
รูปภาพ console output ถูกละเว้น