Design principle of gesture diagram (2) Drag, pinch, swipe, rotate

Source: Internet
Author: User

VIEWCONTROLLER.M#import "ViewController.h"
@interface Viewcontroller ()
{
Uiimageview *imageview;
}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];

ImageView = [[Uiimageview alloc]initwithframe:cgrectmake (200, 200, 200, 200)];
Imageview.image = [UIImage imagenamed:@ "coated. jpg"];
[Self.view Addsubview:imageview];
/*
Gestures are divided into six major gestures:
All six gestures are inherited from Uigesturerecognizer.
1. Click UITapGestureRecognizer
2, Long press Uilongpressgesturerecognizer
3, drag Uipangesturerecognizer
4, kneading Uipinchgesturerecognizer
5. Swipe Uiswipegesturerecognizer
6. Rotating Uirotationgesturerecognizer

Uigesturerecognizer
Initialize gestures
-(Instancetype) Initwithtarget: (nullable ID) Target action: (nullable SEL) action;
How to add gestures in UIView
Addgesturerecognizer:
[xx addgesturerecognizer:xx];
*/
#pragma mark---------dragged---------------------------
Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector (pan:)];
Set the minimum number of fingers
Pan.minimumnumberoftouches = 1;
Set maximum number of fingers
Pan.maximumnumberoftouches = 2;
[Self.view Addgesturerecognizer:pan];


#pragma mark---------Swipe---------------------------
Uiswipegesturerecognizer *swipe = [[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (swipe:)];
swipe.numberoftouchesrequired = 1;

Set the direction of the swipe
/*

Uiswipegesturerecognizerdirectionright
Uiswipegesturerecognizerdirectionleft
Uiswipegesturerecognizerdirectionup
Uiswipegesturerecognizerdirectiondown

*/

Let the swipe be in the right direction
Swipe.direction = Uiswipegesturerecognizerdirectionright;
[Self.view Addgesturerecognizer:swipe];
Cannot respond to gesture conflicts
Wait until a gesture is over and then respond to another gesture.
Wait for a swipe (swipe) response and then respond to the drag (pan)
[Pan Requiregesturerecognizertofail:swipe];

Let the swipe direction be left
Uiswipegesturerecognizer *left = [[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (swipe:)];
Left.direction = Uiswipegesturerecognizerdirectionleft;
[Self.view Addgesturerecognizer:left];
[Pan Requiregesturerecognizertofail:left];

#pragma mark---------kneading---------------------------
Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector (pinch:)];
[Self.view Addgesturerecognizer:pinch];

#pragma mark---------Rotate---------------------------
Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc]initwithtarget:self Action: @selector ( Rotation:)];
[Self.view addgesturerecognizer:rotation];
}

Rotating
-(void) Rotation: (Uirotationgesturerecognizer *) sender
{
Get the rotation angle of the gesture, let the imageview change according to this angle
Imageview.transform = Cgaffinetransformmakerotation (sender.rotation);

}

Kneading
-(void) Pinch: (Uipinchgesturerecognizer *) sender
{
is a view deformation, transform is a property inside the UIView, but make the view change shape, after the deformation of the view, do other things do not restore the original shape, unless using the transfrom inside the Restore View method

@property (nonatomic) Cgaffinetransform transform; Default is cgaffinetransformidentity. Animatable
Cgaffinetransform classes that let the view deform
Cgaffinetransformmakescale (< #CGFloat Sx#>, < #CGFloat sy#>) zoom in and out as you change the view by one scale
Cgaffinetransformmakerotation (< #CGFloat angle#>) to rotate the view by one radian to change
Cgaffinetransformidentity---all patterns changed before restore

Imageview.transform = Cgaffinetransformmakescale (Sender.scale, Sender.scale);
}


Swipe
-(void) Swipe: (Uiswipegesturerecognizer *) sender
{
CGFloat x;
if (sender.direction = = Uiswipegesturerecognizerdirectionleft) {
x = 0.0;
}else{
x = 200.0;
//    }
CGFloat x = sender.direction = = uiswipegesturerecognizerdirectionleft?0:200;

[UIView animatewithduration:0.7 animations:^{
Self.view.frame = CGRectMake (x, 0, Cgrectgetwidth (self.view.frame), Cgrectgetheight (Self.view.frame));
UIButton *button = [[UIButton alloc]initwithframe:cgrectmake (30, 50, 60, 40)];
[Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];
[Self.view Addsubview:button];
}];


}

Dragging
-(void) pan: (Uipangesturerecognizer *) sender
{
Restore a view to its original style
Imageview.transform = cgaffinetransformidentity;
Cgpoint point = [Sender TranslationInView:self.view];
NSLog (@ "x:%f y:%f", POINT.X,POINT.Y);
Point click Position is (0,0) after panning to the left, decreasing upward, can get direction and position Translationinview:

Get the center point of the drag
Cgpoint pancenter = [Sender LocationInView:self.view];
Imageview.center = Pancenter;

}






-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

@end

Design principle of gesture diagram (2) Drag, pinch, swipe, rotate

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.