. h file
#import <UIKit/UIKit.h>
@interface Rootviewcontroller:uiviewcontroller
{
Uiimageview *imageview;
}
@end
. m file
#import "RootViewController.h"
@interface Rootviewcontroller ()
@end
@implementation Rootviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Add a background color to a view
Self.view.backgroundColor =[uicolorwhitecolor];
Create Uiimageview
ImageView =[[uiimageviewalloc]initwithframe:cgrectmake (80, 100, 100, 100)];
Imageview.backgroundcolor =[uicolorredcolor];
[Self.view Addsubview:imageview];
Turn on Interaction
imageview.userinteractionenabled = YES;
Implement a click Method
UITapGestureRecognizer *tap =[[uitapgesturerecognizeralloc]initwithtarget:selfaction: @selector (TapHand:)];
Click Count
tap.numberoftapsrequired = 1;
[ImageView Addgesturerecognizer:tap];
[Tap release];
Implementing a long-press method
Uilongpressgesturerecognizer *longpress =[[uilongpressgesturerecognizeralloc]initwithtarget:selfaction: @selector (longpress:)];
How long does it take to work?
Longpress.minimumpressduration = 2;
Allowable offset distance
longpress.allowablemovement = 100;
[Imageviewaddgesturerecognizer:longpress];
[Longpress release];
Swipe gestures
Uiswipegesturerecognizer *swip =[[uiswipegesturerecognizeralloc]initwithtarget:selfaction: @selector (swipPress:)] ;
Direction of Swipe, (left)
Swip.direction=uiswipegesturerecognizerdirectionleft;
[ImageView ADDGESTURERECOGNIZER:SWIP];
[Swip release];
Rotate gestures
Uirotationgesturerecognizer *rotation =[[uirotationgesturerecognizeralloc]initwithtarget:selfaction: @selector ( ROTATIONP:)];
[Imageviewaddgesturerecognizer:rotation];
[Rotation release];
Drag and Drop method
Uipangesturerecognizer *pan =[[uipangesturerecognizeralloc]initwithtarget:selfaction: @selector (HandlePan:)];
[ImageView Addgesturerecognizer:pan];
[Pan release];
}
click Method
-(void) Taphand: (uitapgesturerecognizer*) Tap
{
}
Long-Press method
-(void) Longpress: (Uilongpressgesturerecognizer *) LONGP
{
if (longp.state = = Uigesturerecognizerstatebegan) {
NSLog (@ "start");
}else if (longp.state = = uigesturerecognizerstatechanged)
{
NSLog (@ "mobile");
}else if (longp.state = = uigesturerecognizerstateended)
{
NSLog (@ "End");
}
}
Swipe method
-(void) Swippress: (uiswipegesturerecognizer*) Swip
{
if (swip.direction==uiswipegesturerecognizerdirectionleft) {
NSLog (@ "picture movement");
}
}
Rotation Gesture Method
-(void) Rotationp: (Uirotationgesturerecognizer *) Rotationp
{
Rotationp.view.transform=cgaffinetransformrotate (RotationP.view.transform, rotationp.rotation);
rotationp.rotation = 0;
}
Drag gesture Method
-(void) Handlepan: (uipangesturerecognizer*) recognizer
{
Cgpoint translation = [recognizer TranslationInView:self.view];
Recognizer.view.center=cgpointmake (recognizer.view.center.x+translation.x,recognizer.view.center.y+ TRANSLATION.Y);
[Recognizer SetTranslation:CGPointZeroinView:self.view];
if (recognizer.state==uigesturerecognizerstateended) {
Cgpoint velocity = [recognizer VelocityInView:self.view];
CGFloat magnitude = sqrtf ((velocity.x * velocity.x) + (VELOCITY.Y * velocity.y));
CGFloat slidemult = magnitude/200;
NSLog (@ "magnitude:%f, Slidemult:%f", magnitude, slidemult);
float Slidefactor = 0.1*slidemult;//increase for more a slide
Cgpoint finalpoint = Cgpointmake (recognizer.view.center.x + (velocity.x * slidefactor), Recognizer.view.center.y + ( VELOCITY.Y * slidefactor));
Finalpoint.x = MIN (MAX (finalpoint.x, 0), self.view.bounds.size.width);
Finalpoint.y = MIN (MAX (finalpoint.y,0), self.view.bounds.size.height);
[Uiviewanimatewithduration:slidefactor*2 delay:0options:uiviewanimationoptioncurveeaseoutanimations:^{ Recognizer.view.center=finalpoint;} Completion:nil];
}
}
This article is from the iOS blog, so be sure to keep this source http://10136044.blog.51cto.com/10126044/1679358
IOS gesture App