Add gestures to UIView

Source: Internet
Author: User

For UI objects that cannot be addtarget, adding gestures brings them the "gospel", as an example of uiview adding gestures to uncover gestures.

1, create a view first,

UIView * Jrview=[[uiviewalloc] Initwithframe:cgrectmake (00)];    Jrview.center=self.view.center;    Jrview.backgroundcolor=[Uicolorgreencolor];    [Self.viewaddSubview:jrView];

2, adding gestures

Common gestures: Tap, hold, sweep (surface slip over), drag, rotate, pinch.

①: Click

First, define the click Gesture and then set the related properties of the gesture (number of clicks, Number of touchpoints ...). ), and then add the gesture to the view.

  Click on the view to perform gestures @selector ( Tapgestureaction:) method (all gestures are the same);

/* * * * tap=[[Uitapgesturerecognizeralloc] initwithtarget:selfaction: @selector (tapgestureaction:)]; // -=--Click Count    tap.numberoftapsrequired=2; // =-=-= number    of touch points tap.numberoftouchesrequired=2; // tap.numberoftouches;  ReadOnly    [Jrview Addgesturerecognizer:tap];

②: Long Press

Defining gestures

Properties: A long-pressed area, a long-pressed time

Add gesture to view

/* * * * lpress=[[Uilongpressgesturerecognizeralloc] initwithtarget:selfaction: @selector (longpressaction:)]; // ===--= The recognition area of the long press (even if it is moved, long Press)    lpress.allowablemovement=; // =-=-==-pressed Time-(by how long to press)    lpress.minimumpressduration=0.5;    [Jrview addgesturerecognizer:lpress];

③: Dragging

is so concise;

/* * * * pan=[[Uipangesturerecognizeralloc] initwithtarget:selfaction: @selector (panaction:)];    [Jrview Addgesturerecognizer:pan];

④: Swipe (surface slip over )

can set the direction of the slide, different directions to trigger different events;

/* * Swipe */ * swipe= [[uiswipegesturerecognizeralloc] initwithtarget:selfaction     : @selector (swipeaction:)];    Swipe.direction=uiswipegesturerecognizerdirectionleft;    [Jrview Addgesturerecognizer:swipe];

⑤: Rotate

There are agents, please check the agency agreement;

/* *  Rotate   */ * rotation=[[Uirotationgesturerecognizeralloc] Initwithtarget: Selfaction: @selector (rotationaction:)];    Rotation. delegate= self;    [Jrview addgesturerecognizer:rotation];

⑥: Kneading

To pinch, two fingers to operate (reasonable);

/* *  kneading   /* pinch=[[Uipinchgesturerecognizeralloc] Initwithtarget:selfaction : @selector (pinchaction:)];    Pinch. delegate= self;    [Jrview Addgesturerecognizer:pinch];

at the same time, the same view can be set to respond to multiple gestures, so there is the delegate of gestures;

#pragma The mark-proxy method, which causes the operand to respond to multiple gestures-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer{returnyes ;}

Gesture? So easy!

Add gestures to UIView

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.