IOS gesture App

Source: Internet
Author: User
Tags uikit

. 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

Related Article

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.