Examples of iOS gesture applications

Source: Internet
Author: User

/**
1. Four steps to use gesture recognition

1> instantiation gesture Recognition
-(ID) Initwithtarget: (ID) Target action: (SEL) Action;
2> Setting gesture Recognition properties
3> attaching gesture recognition to a specified view
Addgesturerecognizer

4> writing a listening method

Create a Uiimageview with code to add to the view, experiment with 6 gesture recognition.

3. Gesture Description

0> Uigesturerecognizer All gesture recognition of the parent class, not allowed to use directly, can be used to customize the gesture (recommend that you do not challenge)

Most commonly used properties are:

View: gesture-taking views
State: The current status of the gesture, mainly for continuous gestures, not used for discrete gestures



1> UITapGestureRecognizer tap gestures (discrete gestures, other gestures are continuous gestures)

Property:
numberoftapsrequired clicks, click Double Tap
numberoftouchesrequired Finger Root number

2> Uilongpressgesturerecognizer Long Press gesture

property is not recommended for modification

3> Uipangesturerecognizer Drag gesture

Properties: Infrequently used
Method:
The distance dragged in the view
-(Cgpoint) Translationinview: (UIView *) view;
The speed at which you drag in a view is often used to simulate inertia and requires some physical computing
-(Cgpoint) Velocityinview: (UIView *) view;

4> Uipinchgesturerecognizer Pinch gesture

Property:
Scale ratio
Velocity kneading speed, not commonly used

5> Uirotationgesturerecognizer Rotation gesture

Property
Rotation rotation angle
Velocity rotational speed, not commonly used

6> Uiswipegesturerecognizer swipe gesture, usually added to the root view

Property
numberoftouchesrequired number of finger roots participating in a swipe gesture
Direction the direction of the swipe

Tips:
1) If you want to detect a swipe in several directions, you need to instantiate a few swipe gestures separately
2) swipe gesture is a continuous gesture, but does not need to handle the uigesturerecognizerstatechanged state
This gesture is only recognized when the finger leaves the screen.

Other Tips

In the If statement, the constant is placed on the left side of the judging condition, you can avoid the situation because only write = warning appears, ignore it!
*/

-(void) viewdidload
{
[Super Viewdidload];

0. Instantiate Uiimageview
UIImage *image = [UIImage imagenamed:@ "001"];
Uiimageview *imageview = [[Uiimageview alloc]initwithimage:image];

[ImageView Setframe:cgrectmake (10, 100, 300, 196)];
[Self.view Addsubview:imageview];

[ImageView Setuserinteractionenabled:yes];

1. Add a tap gesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tap:)];
Define double-click
tap.numberoftapsrequired = 2;
[ImageView Addgesturerecognizer:tap];

2. Add a long press gesture
Uilongpressgesturerecognizer *longtap = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector ( Longtap:)];
[ImageView Addgesturerecognizer:longtap];

3. Drag gestures
Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)];

[ImageView Addgesturerecognizer:pan];

4. Pinch gesture
Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (pinch:)];
[ImageView Addgesturerecognizer:pinch];

5. Rotate gestures
Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector ( Rotate:)];
[ImageView addgesturerecognizer:rotation];

6. Swipe gestures
If you want to do a sweep gesture, you need to instantiate the gestures in 4 directions separately.
Uiswipegesturerecognizer *swipeup = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe:)];

Swipeup.direction = Uiswipegesturerecognizerdirectionup | Uiswipegesturerecognizerdirectiondown | Uiswipegesturerecognizerdirectionleft | Uiswipegesturerecognizerdirectionright;

[Self.view Addgesturerecognizer:swipeup];

Uiswipegesturerecognizer *swipedown = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe :)];
Swipedown.direction = Uiswipegesturerecognizerdirectiondown;
[Self.view Addgesturerecognizer:swipedown];
//
Uiswipegesturerecognizer *swipeleft = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe :)];
Swipeleft.direction = Uiswipegesturerecognizerdirectionleft;
[Self.view Addgesturerecognizer:swipeleft];
//
Uiswipegesturerecognizer *swiperight = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipe:)];
Swiperight.direction = Uiswipegesturerecognizerdirectionright;
[Self.view Addgesturerecognizer:swiperight];
}

#pragma mark-tap gestures
-(void) Tap: (UITapGestureRecognizer *) recognizer
{
Double click to enlarge picture
Uiimageview *imageview = (Uiimageview *) Recognizer.view;

[UIView animatewithduration:0.8f animations:^{
[ImageView Settransform:cgaffinetransformscale ( Imageview.transform, 2.0, 2.0)];
} completion:^ (BOOL finished) {
[UIView animatewithduration:0.5f animations:^{
[ImageView settransform: Cgaffinetransformscale (Imageview.transform, 0.5, 0.5)];
}];
}];
}

#pragma mark-long-press gesture
-(void) Longtap: (Uilongpressgesturerecognizer *) recognizer
{
NSLog (@ "long press");
// Long press rotate picture
//start long press
//In the deformation, iOS to take the nearest principle of rotation, if not in the direction of the desired rotation, you can add some correction values, such as 0.01
//directly through the deformation properties, to achieve a one-time turn, it is difficult, can be divided two times
if (Uigesturerecognizerstatebegan = = recognizer.state) {
[UIView animatewithduration:0.8f animations:^{
[ Recognizer.view settransform:cgaffinetransformrotate (Recognizer.view.transform, m_pi-0.01)];
}];
} else if (uigesturerecognizerstateended = = recognizer.state) {
[UIView animatewithduration:0.8f animations:^{< br> [Recognizer.view settransform:cgaffinetransformrotate (Recognizer.view.transform, m_pi-0.01)];
} completion:^ (BOOL finished) {
//Clears all deformation properties
[Recognizer.view settransform:cgaffinetransformidentity];
} ];
}
}

#pragma mark-Drag
-(void) pan: (Uipangesturerecognizer *) recognizer
{
Cgpoint translation = [recognizer TranslationInView:self.view];

Let the image move according to your finger
if (uigesturerecognizerstatechanged = = recognizer.state) {
Deformation parameters, with make, are calculated relative to the initial position
No make is incremented to modify the deformation
[Recognizer.view settransform:cgaffinetransformtranslate (Recognizer.view.transform, translation.x, TRANSLATION.Y) ];

The translation distance in the drag finger is relative to the initial position, if the cgaffinetransformtranslate cumulative deformation method is used
It is necessary to reset the displacement of the recognizer after each displacement, that is, to clear the displacement amount
[Recognizer Settranslation:cgpointzero InView:recognizer.view];
}
}

#pragma mark-kneading
-(void) Pinch: (Uipinchgesturerecognizer *) recognizer
{
Adjust the display scale of an image
if (uigesturerecognizerstatechanged = = recognizer.state) {

Equal scale Scaling
[Recognizer.view Settransform:cgaffinetransformscale (Recognizer.view.transform, Recognizer.scale, Recognizer.scale)];

The scale of the top-up gesture is required when setting the cumulative deformation parameter
Recognizer.scale = 1.0f;
}
}

#pragma mark-Rotate
-(void) Rotate: (Uirotationgesturerecognizer *) recognizer
{
if (uigesturerecognizerstatechanged = = recognizer.state) {
Recognizer.view.transform = Cgaffinetransformrotate (Recognizer.view.transform, recognizer.rotation);

Reset Rotation Amount
Recognizer.rotation = 0.0f;
}
}


#pragma mark-Swipe
-(void) Swipe: (Uiswipegesturerecognizer *) recognizer
{
NSLog (@ "%d", recognizer.direction);
if (Uiswipegesturerecognizerdirectiondown = = recognizer.direction) {
NSLog (@ "down");
} else if (Uiswipegesturerecognizerdirectionup = = recognizer.direction) {
NSLog (@ "up");
} else if (Uiswipegesturerecognizerdirectionleft = = recognizer.direction) {
NSLog (@ "left");
} else if (uiswipegesturerecognizerdirectionright = = recognizer.direction) {
NSLog (@ "right");
}
}

Examples of iOS gesture applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.