Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Iphone Application : Using Gesture Recognizers (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/8/2012 5:08:26 PM

Creating the Interface

Open the GesturesViewController.xib file in Interface Builder. It’s time to create our UI.

As I mentioned when we started, we don’t have to concern ourselves with any of the typical actions or events in this project. We’ll add some objects, and then connect their outlets.

To build the interface, start by dragging four UIView instances to the main view. Size the first to a small rectangle in the upper-right portion of the screen; it will capture taps. Make the second a long rectangle beside the first (for detecting swipes). Size the other two views as large rectangles below the first two (for pinches and rotations). Use the Attributes Inspector (Command+1) to set the background of each view to be something unique.

Did you Know?

The views you are adding are convenient objects that we can attach gestures to. In your own applications, you can attach gesture recognizers to your main application view or the view of any onscreen object.


Did you Know?

Gesture recognizers work based on the starting point of the gesture, not where it ends. In other words, if a user uses a rotation gesture that starts in a view but ends outside the view, it will work fine. The gesture won’t “stop” just because it crosses a view’s boundary.

For you, the developer, this is a big help for making multitouch applications that work well on a small screen.


Next, drag labels into each of the four views. The first label should read Tap Me!. The second should read Swipe Me!. The third label should read Pinch Me!. The fourth label should read Rotate Me!.

Drag a fifth UILabel instance to the main view, and center it at the top of the screen. Use the Attributes Inspector to set it to align center. This will be the label we use to provide feedback to the user. Change the label’s default text to Do something!.

Finally, add a UIImageView to the bottom center of the screen. Use the Attributes Inspector (Command+1) and Size Inspector (Command+3) to set the image to flower.png and the size and location to X: 98.0, Y:328.0, W:123.0, H:112.0.

The finished view should resemble Figure 4.

Figure 4. Your final view should look like this.


Connecting the Outlets

To access our gesture views and feedback objects from the main application, we need to connect the outlets defined earlier. Starting with the top-left view, Control-drag from the File’s Owner icon to the view. Choose the tapView outlet when prompted, as shown in Figure 5.

Figure 5. Connect the four views, labels, and image views to their outlets.


Repeat this process, connecting the top-right view to the swipeView outlet, the bottom-left view to pinchView, and the bottom-right view to rotateView. Connect the top UILabel to outputLabel and the bottom UIImageView to imageView.

We can now close the XIB file and exit Interface Builder. It’s time to connect our gesture recognizers.

Implementing the Tap Gesture Recognizer

We’re going to start implementing the gesture recognizer logic by implementing the “tap” recognizer. What you’ll quickly discover is that after you’ve added one recognizer, the pattern is very, very similar for the others.

Creating the Recognizer

The first thing to consider is this: Where do we want to instantiate a gesture recognizer? For this project, it makes sense that the user can start inputting gestures as soon as the application starts and the views are visible. So the viewDidLoad method is a good spot. Update the GestureViewController.m file so that the viewDidLoad reads as displayed in Listing 2.

Listing 2.
 1: - (void)viewDidLoad {
 2:     [super viewDidLoad];
 3:
 4:     //Taps
 5:     UITapGestureRecognizer *tapRecognizer;
 6:     tapRecognizer=[[UITapGestureRecognizer alloc]
 7:                        initWithTarget:self
 8:                        action:@selector(foundTap:)];
 9:     tapRecognizer.numberOfTapsRequired=1;
10:     tapRecognizer.numberOfTouchesRequired=1;
11:     [tapView addGestureRecognizer:tapRecognizer];
12:     [tapRecognizer release];
13: }

Line 5 kicks things off by declaring an instance of the UITapGestureRecognizer object, tapRecognizer. In line 6, tapRecognizer is allocated and initialized with initWithTarget:action. Working backward, the action is the method that will be called when the tap occurs. Using @selector(foundTap:), we tell the recognizer that we want to use a method called foundTap to handle our taps. The target we specify, self, is the object where foundTap lives. In this case, it will be our view controller (GestureViewController) object, or self.

Lines 9–10 set two properties of the tap gesture recognizer:

NumberOfTapsRequired: The number of times the object needs to be tapped before the gesture is recognized

NumberOfTouchesRequired: The number of fingers that need to be down on the screen before the gesture is recognized

In this example, we’re defining a “tap” as one finger tapping the screen once. Feel free to play with these properties as much as you like!

In Line 11, we use the UIView method addGestureRecognizer to add the tapRecognizer to the tapView. Our view is now tap-aware, and we can release tapRecognizer in line 12.

The next step is to add the code to respond to a tap event.

Responding to the Recognizer

Responding to the tap gesture recognizer is just a matter of implementing the foundTap method. Add this new method to the GestureViewController.m file, as follows:

- (void)foundTap:(UITapGestureRecognizer *)recognizer {
    outputLabel.text=@"Tapped";
}

Ta da! Your first gesture recognizer is done! We’ll repeat this process for the other four, and we’ll be finished before you know it!

Did you Know?

If you want to get the coordinate where a tap gesture (or a swipe) takes place, you add code like this to the gesture handler (replacing <the view> with the name of the recognizer’s view):

CGPoint location = [recognizer locationInView:<the view>];

This will create a simple structure named location, with members x and y, accessible as location.x and location.y.

Other -----------------
- Handling Input on Windows Phone 7 : Microphone Input
- Handling Input on Windows Phone 7 : Accelerometer
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 2) - Getting a Device
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 1) - Recreating the Project on Xbox, Devices and Containers
- XNA Game Studio 4.0 : Storage - Isolated Storage
- Using Media in XNA Game Studio : Visualizations
- Using Media in XNA Game Studio : Video
- XNA Game Studio 4.0 : Dynamic Sound Effects - Recording Audio with a Microphone, Generating Dynamic Sound Effects
- XNA Game Studio 4.0 : Playing Sound Effects (part 2) - Microsoft Cross-Platform Audio Creations Tool
- XNA Game Studio 4.0 : Playing Sound Effects (part 1) - Using SoundEffect for Audio Playback
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server