Touchandguest Touch Events and gestures

Source: Internet
Author: User
Tags ontap

//
Nextviewcontroller.m
#import "NextViewController.h"
#import "My.h"
@interface Nextviewcontroller ()

@end

@implementation Nextviewcontroller
-(void) Viewdidload {

[Super Viewdidload];

[self createview];

}




#pragma Mark------------------------CreateView
-(void) CreateView
{
Self.navigationItem.title [email protected] "Gestur";

Uiimageview *vi =[[uiimageview alloc] init];

Vi.tag = 100;

Vi.frame = CGRectMake (100, 100, 120, 120);

Vi.image = [UIImage imagenamed:@ "a"];

[Self.view Addsubview:vi];

vi.userinteractionenabled = YES;

#pragma mark------------------------Click Gestures
One view can attach multiple gestures, one gesture can only be attached to one view
Click Gestures
UITapGestureRecognizer *tap =[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (ONTAP:)];

The number of clicks is 1 by default
tap.numberoftapsrequired = 1;

Need a few fingers click Default is 1
tap.numberoftouchesrequired = 1;

Attach to view
[VI Addgesturerecognizer:tap];

#pragma mark------------------------Double-click Gestures
Double-click Gestures
UITapGestureRecognizer *doubletap=[[uitapgesturerecognizer Alloc]
Initwithtarget:self Action: @selector (ondouble:)];

doubletap.numberoftapsrequired = 2;

[VI Addgesturerecognizer:doubletap];

//Click to recognize gestures after double-tap gesture recognition failed
[Tap Requiregesturerecognizertofail:doubletap];

#pragma mark------------------------Long press the gesture
Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (onpress:)];

Longpress.minimumpressduration = 1;

[VI addgesturerecognizer:longpress];

#pragma mark------------------------drag gesture
Drag gestures Pan
Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Onpan:)];

[VI Addgesturerecognizer:pan];

#pragma mark------------------------pinch gesture
Pinch gesture
Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc]
Initwithtarget:self Action: @selector (onpinch:)];
[VI Addgesturerecognizer:pinch];

Compliance with Uigesturerecognizerdelegate Protocol
Pinch.delegate = self;

#pragma mark------------------------rotation gesture
Rotate gestures
Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector ( Onrotation:)];

[VI addgesturerecognizer:rotation];

Rotation.delegate = self;

Swipe gesture #pragma mark------------------------
//*********************************************************************
Uiswipegesturerecognizer *swipedown = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (OnSwipe :)];

Set the direction of the slide
Swipedown.direction = Uiswipegesturerecognizerdirectiondown;

[VI Addgesturerecognizer:swipedown];

//*********************************************************************

Uiswipegesturerecognizer *swipeleft = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (OnSwipe :)];

Swipeleft.direction = Uiswipegesturerecognizerdirectionleft;

[VI Addgesturerecognizer:swipeleft];

//*********************************************************************

Uiswipegesturerecognizer *swiperight = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Onswipe:)];

Swiperight.direction = Uiswipegesturerecognizerdirectionright;

[VI Addgesturerecognizer:swiperight];

//*********************************************************************

Uiswipegesturerecognizer *swipeup = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (onSwipe:) ];

Swipeup.direction = Uiswipegesturerecognizerdirectionup;

[VI Addgesturerecognizer:swipeup];


}


#pragma mark------------------------Double-click Gestures
-(void) Ondouble: (UITapGestureRecognizer *) ondouble
{
NSLog (@ "Picture is double-clicked ...");
}

#pragma mark------------------------Click
-(void) OnTap: (UITapGestureRecognizer *) tap
{
NSLog (@ "Picture clicked ...");

[self creatuimenucontroller:tap];
}

#pragma mark------------------------Uimenucontroller
-(void) Creatuimenucontroller: (UITapGestureRecognizer *) tap
{

Menu is a singletonnsuserdefaults uiapplication
UimenucontrolleR *menu = [Uimenucontroller Sharedmenucontroller];

UIMenuItem * item1 = [[UIMenuItem alloc] initwithtitle:@ "copy" Action: @selector (oncopy)];

UIMenuItem * item2 = [[UIMenuItem alloc] initwithtitle:@ "Delete" Action: @selector (OnDelete)];

Menu.menuitems = @[item1,item2];

Cgpoint point = [Tap LocationInView:self.view];

NSLog (@ "point.x=%f", point.x);
NSLog (@ "point.y=%f", POINT.Y);

Set coordinates
[Menu Settargetrect:cgrectmake (Point.x, Point.y, () InView:self.view];

Show Menu
[Menu Setmenuvisible:yes Animated:yes];

[Self becomefirstresponder];
}

-(void) oncopy
{

}

-(void) OnDelete
{
Uiimageview *IV = (ID) [Self.view viewwithtag:100];
[IV Removefromsuperview];
}

-(BOOL) Canbecomefirstresponder
{
return YES;
}
#pragma mark------------------------long pressure
-(void) Onpress: (UITapGestureRecognizer *) tap
{
NSLog (@ "Picture is long pressed");
}

#pragma mark------------------------drag
-(void) Onpan: (UITapGestureRecognizer *) tap
{
NSLog (@ "Picture is dragged ...");

Uiimageview *IV = (ID) [Self.view viewwithtag:100];

Find the dragged position
Cgpoint point = [Tap LocationInView:self.view];

if (point.x>=self.view.bounds.size.width-10)
Point.x = self.view.bounds.size.width-10;

Iv.frame = CGRectMake (Point.x, Point.y, Iv.frame.size.width, iv.frame.size.height);

NSLog (@ "%f,%f", POINT.X,POINT.Y);

Iv.center = point;
}

#pragma mark------------------------pinch gesture
-(void) Onpinch: (Uipinchgesturerecognizer *) pinch
{
NSLog (@ "pinch gesture");

Zoom in and Zoom out
Uiimageview *VI = (ID) [Self.view viewwithtag:100];

Vi.transform = Cgaffinetransformscale (Vi.transform, Pinch.scale, Pinch.scale); The scaling y of X is relative to the scale of the

[Pinch setscale:1]; Scale 1 is equivalent to not scaling
}

#pragma mark------------------------rotate
-(void) Onrotation: (Uirotationgesturerecognizer *) rot
{
NSLog (@ "rotation gesture");

Uiimageview *VI = (ID) [Self.view viewwithtag:100];

Vi.transform = Cgaffinetransformrotate (Vi.transform, rot.rotation);

[Rot setrotation:0];
}

//pan The swipe function is not available when dragging events
#pragma Mark------------------------Swipe
-(void) Onswipe: (Uiswipegesturerecognizer *) swipe
{

Uiimageview *VI = (ID) [Self.view viewwithtag:100];

Cgpoint point = [swipe locationInView:self.view];

if (swipe.direction = = Uiswipegesturerecognizerdirectiondown)
{
static int i = 0;
NSLog (@ "swipe gesture swip down =%d Times", ++i);

Vi.frame = CGRectMake (point.y+2, Vi.frame.size.width, vi.frame.size.height);
}
else if (swipe.direction = = Uiswipegesturerecognizerdirectionleft)
{
static int i = 0;
NSLog (@ "swipe gesture swip left =%d times", ++i);
}
else if (swipe.direction = = uiswipegesturerecognizerdirectionright)
{
static int i = 0;
NSLog (@ "swipe gesture swip right =%d times", ++i);
}
else if (swipe.direction = = Uiswipegesturerecognizerdirectionup)
{
static int i = 0;

NSLog (@ "swipe gesture swip up =%d Times", ++i);//???????
NSLog (@ "point.y=%f", POINT.Y);
Vi.frame = CGRectMake (point.y-200, Vi.frame.size.width, vi.frame.size.height);

NSLog (@ "point.y=%f", POINT.Y);
}


}

#pragma mark------------------------implement a proxy protocol similar to the method//<UIGestureRecognizerDelegate>
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer
{

Allows similar gestures to accept events if the method returns Yes to allow similar gesture recognition events
return YES;
}
@end

Touchandguest Touch Events and gestures

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.