Search code examples
uibuttonuievent

Swift: Haptic effects for grid of UIButtons


I have a grid of UIButtons and I'd like to call a haptic effect for the following methods:

  1. When the user moves their finger over a button for the first time
  2. When the user lifts their finger (a typical tap, .touchUpInside works perfectly)

Any suggestions for how to call a haptic effect when the user moves over a button for the first time?

    button.addTarget(self, action: #selector(tap), for: .touchUpInside)
    button.addTarget(self, action: #selector(moveOver), for: .??)

func moveOver(sender:UIButton) -> Bool {
        let generator = UIImpactFeedbackGenerator(style: .heavy)
        generator.impactOccurred()
        return true
}

Solution

  • The correct UIEventAction to use in this case is touchDragInside