Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> iOS

ตรวจจับ Swipe แนวตั้งบน ScrollView ใน Swift

ScrollView ไม่มี delegate ให้ทิศทาง scroll โดยตรง ใช้ UISwipeGestureRecognizer แทน

ขั้นตอนใน Storyboard

  1. ลาก Swipe Gesture Recognizer วางเหนือ Scroll View
  2. Attribute Inspector → Swipe → ตั้งค่า Direction: Up (หรือ Down)
  3. สร้าง 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 ถูกละเว้น