background preloader

Gesture

Facebook Twitter

Handling touch events for uiButtons in iPhone. UIImageView + Touch Handling = UIButton. Gdscei asks: “Hello, how can i make an image view work as a button?

UIImageView + Touch Handling = UIButton

I want it to be assigned to the ‘reload’ action of WebView. Can someone give me instructions how to do this?” Often people start out constructing their UI visually, starting with this line of reasoning “I want to show an image. Ah, UIImageView”. So if we create a new view-based project in XCode, we could modify the viewDidLoad of the main view controller like this to show the image: The next logical step in reasoning right after displaying the icon is now to get touch handling somehow.

Sure, if you consider yourself extra smart and that you won’t be stumped by such an inconvenient “cul de sac” then you will proceed to subclass UIImageView and adding touch handling via the touchesBegan, touchesMoved, touchesCancelled and touchesEnded method. This post is NOT about doing that. I already gave away half of the solution: CUSTOM BUTTONS. UIButtonType can have these values: But UIImageView and UIButton are views!

Like this: Gestures Recognizers – Tap, Pinch/Zoom, Rotate, Swipe, Pan, Long Press. The UIGestureRecognizer class is available to help with detecting and responding to the various UI gestures common on iOS devices.

Gestures Recognizers – Tap, Pinch/Zoom, Rotate, Swipe, Pan, Long Press

UIGestureRecognizer is an abstract class, with the following concrete subclasses, one for each type of available recognizer: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UISwipeGestureRecognizer UIPanGestureRecognizer UILongPressGestureRecognizer Depending on the type of recognizer, there are various behaviors that you can configure. For instance, with the UITapGestureRecognizer, you can specify the number of taps and number of touches. As an example, you could specify that two fingers are required, tapping twice with both fingers. In response to recognized gestures, an action messages is sent to a target object that you specify.

Once you’ve defined a gesture, you add the recognizer to the view. Gesture Recognizer Examples Let’s walk through configuration of a few gestures to see how all this works. And finally, two finger pinch: iPhone iOS4 iPad SDK Development & Programming Blog. The application we are about to build does nothing more than detect swipes, both horizontal and vertical.

iPhone iOS4 iPad SDK Development & Programming Blog

Swipes will display a message across the top of the screen for a few seconds informing you swipe was detected. We are going to define a minimum gesture length in pixels, which is how far the user has to swipe before the gesture counts as a swipe. Step 1: Create a new project in Xcode using the View-based application template. Step 2: We start by defining a minimum gesture length of 25 pixels and a variance 0f 5. If the user was doing a horizontal swipe, the gesture could end up 5 pixels above or below the starting vertical position and still count as a swipe as long as the user moved 25 pixels horizontally.