iOS development UI Chapter-gesture recognizer (Long press + swipe)

Source: Internet
Author: User

iOS development UI Chapter-gesture recognizer (Long press + swipe)

One, long press events

1 //2 //YYVIEWCONTROLLER.M3 //03-Long Press4 //5 //Created by Apple on 14-6-19.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A@property (Weak, nonatomic) Iboutlet UIView *IconView; -  - @end the  - @implementationYyviewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; +      A     //Long Press Events at     //1. Create a gesture Recognizer Object -Uilongpressgesturerecognizer *longpress=[[Uilongpressgesturerecognizer alloc]init]; -  -     //2. Set the properties of the long-press gesture recognizer -     //set minimum dwell time - //longpress.minimumpressduration=3; in     //the offset at which the finger is allowed to move before the event responds -Longpress.allowablemovement= -; to      +     //3. Add gesture recognizer to view - [Self.iconview addgesturerecognizer:longpress]; the      *     //4. Monitor gesture recognizer $ [longpress addtarget:self Action: @selector (Longpressview)];Panax Notoginseng } -  the-(void) Longpressview + { ANSLog (@"a long-press event has occurred"); the } +  - @end

Steps

(1) Creating a gesture recognizer

(2) Add gesture recognizer

(3) Monitoring

Second, the light sweep event

1 //2 //YYVIEWCONTROLLER.M3 //04- Swipe4 //5 //Created by Apple on 14-6-19.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A@property (Weak, nonatomic) Iboutlet UIView *IconView; -  - @end the  - @implementationYyviewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; +      A     //1. Create a gesture recognizer (swipe event) atUiswipegesturerecognizer *swipe=[[Uiswipegesturerecognizer alloc]init]; -      -     //2. Set the gesture Recognizer's properties -     //Default to right -     //set your finger to swipe to the right to trigger - //Swipe.direction=uiswipegesturerecognizerdirectionup; in     //set swipe down and up two directions -swipe.direction=uiswipegesturerecognizerdirectiondown|Uiswipegesturerecognizerdirectionup; to      +      -     //3. Add gesture recognizer the [Self.iconview addgesturerecognizer:swipe]; *      $     //4. Listening gesturesPanax Notoginseng [Swipe addtarget:self action: @selector (Swipeview)]; - } the  +-(void) Swipeview A { theNSLog (@"Swipe your finger on the screen"); + } -  $ @end

Tip:swipe.direction=uiswipegesturerecognizerdirectiondown| Uiswipegesturerecognizerdirectionup; This method only supports sweeping in one direction at most.
Set up a swipe event that supports four directions, with the following code:

1 //2 //YYVIEWCONTROLLER.M3 //05-Swipe 24 //5 //Created by Apple on 14-6-19.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A@property (Strong, nonatomic) iboutlet UIView *IconView; -  - @end the  - @implementationYyviewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; +      A     //set a swipe in four directions at     //left -Uiswipegesturerecognizer *swipeleft=[[Uiswipegesturerecognizer alloc]init]; -     //set the direction of the swipe -swipeleft.direction=Uiswipegesturerecognizerdirectionleft; - [Self.iconview Addgesturerecognizer:swipeleft]; - [Swipeleft addtarget:self Action: @selector (Swipeleftview)]; in      -     //Right toUiswipegesturerecognizer *swiperight=[[Uiswipegesturerecognizer alloc]init]; +     //set the direction of the swipe -swiperight.direction=Uiswipegesturerecognizerdirectionright; the [Self.iconview addgesturerecognizer:swiperight]; * [swiperight addtarget:self Action: @selector (Swiperightview)]; $     Panax Notoginseng     //downward -Uiswipegesturerecognizer *swipedown=[[Uiswipegesturerecognizer alloc]init]; the        //set the direction of the swipe +swipedown.direction=Uiswipegesturerecognizerdirectiondown; A [Self.iconview Addgesturerecognizer:swipedown]; the [Swipedown addtarget:self Action: @selector (Swipedownview)]; +      -     //Upward $Uiswipegesturerecognizer *swipeup=[[Uiswipegesturerecognizer alloc]init]; $        //set the direction of the swipe -swipeup.direction=Uiswipegesturerecognizerdirectionup; - [Self.iconview addgesturerecognizer:swipeup]; the [Swipeup addtarget:self Action: @selector (Swipeupview)]; -     Wuyi     //A shorthand notation the [Self.iconview addgesturerecognizer: [[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (ABC )] ]; -      Wu } -  About-(void) ABC $ { - } --(void) Swipeleftview - { ANSLog (@"Swipe left with your finger"); + } the  --(void) Swiperightview $ { theNSLog (@"Swipe right with your finger"); the } the-(void) Swipedownview the { -NSLog (@"Swipe down with your finger"); in } the-(void) Swipeupview the { AboutNSLog (@"Swipe up on the finger"); the } the @end

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.