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