IOS-move gesture to add

Source: Internet
Author: User

IOS-move gesture to add
IOS-sliding gesture Add 1. Create a Single View Application project, select next, and name the project swipeGestureTest

 

 

2. Modify the ViewController class file (1) in the ViewController. h file, add Properties

 

@ Property (nonatomic, strong) UISwipeGestureRecognizer * left;

@ Property (nonatomic, strong) UISwipeGestureRecognizer * right;

@ Property (nonatomic, strong) UISwipeGestureRecognizer * up;

@ Property (nonatomic, strong) UISwipeGestureRecognizer * down;

 

@ Property (nonatomic, strong) UILabel * swipeLabel;


 

(2) In the ViewController. m file, add the code

 

@ Synthesize left;

@ Synthesize up;

@ Synthesize right;

@ Synthesize down;


 

(3)-(void) in the viewDidLoad function, add the code

 

 

Self. left = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (handleSwipes :)];

Self. left. direction = uiswipegesturerecognizerdireleft left;

[Self. view addGestureRecognizer: self. left];

 

 

Self. right = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (handleSwipes :)];

Self. right. direction = UISwipeGestureRecognizerDirectionRight;

[Self. view addGestureRecognizer: self. right];

 

Self. up = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (handleSwipes :)];

Self. up. direction = UISwipeGestureRecognizerDirectionUp;

[Self. view addGestureRecognizer: self. up];

 

Self. down = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (handleSwipes :)];

Self. down. direction = UISwipeGestureRecognizerDirectionDown;

[Self. view addGestureRecognizer: self. down];

 

 

Self. swipeLabel = [[UILabel alloc] initWithFrame: CGRectMake (50, 50,200, 20)];

Self. swipeLabel. text = @ "Label ";

[Self. swipeLabel setBackgroundColor: [UIColor greenColor];

[Self. view addSubview: self. swipeLabel];

(4) Add a gesture Response Function

 

-(Void) handleSwipes :( UISwipeGestureRecognizer *) sender

{

If (sender. direction = UISwipeGestureRecognizerDirectionLeft ){

CGPoint labelPosition = CGPointMake (self. swipeLabel. frame. origin. x-10.0, self. swipeLabel. frame. origin. y );

Self. swipeLabel. frame = CGRectMake (labelPosition. x, labelPosition. y, self. swipeLabel. frame. size. width, self. swipeLabel. frame. size. height );

Self. swipeLabel. text = @ "you are sliding to the left ....";

}

If (sender. direction = UISwipeGestureRecognizerDirectionRight ){

CGPoint labelPosition = CGPointMake (self. swipeLabel. frame. origin. x + 10.0, self. swipeLabel. frame. origin. y );

Self. swipeLabel. frame = CGRectMake (labelPosition. x, labelPosition. y, self. swipeLabel. frame. size. width, self. swipeLabel. frame. size. height );

Self. swipeLabel. text = @ "you are sliding to the right ....";

}

If (sender. direction = UISwipeGestureRecognizerDirectionUp ){

CGPoint labelPosition = CGPointMake (self. swipeLabel. frame. origin. x, self. swipeLabel. frame. origin. y-10.0 );

Self. swipeLabel. frame = CGRectMake (labelPosition. x, labelPosition. y, self. swipeLabel. frame. size. width, self. swipeLabel. frame. size. height );

Self. swipeLabel. text = @ "you are sliding up ....";

}

If (sender. direction = UISwipeGestureRecognizerDirectionDown ){

CGPoint labelPosition = CGPointMake (self. swipeLabel. frame. origin. x, self. swipeLabel. frame. origin. y + 10.0 );

Self. swipeLabel. frame = CGRectMake (labelPosition. x, labelPosition. y, self. swipeLabel. frame. size. width, self. swipeLabel. frame. size. height );

Self. swipeLabel. text = @ "you slide down ....";

}

 

}

3. Run
Source code link: http://download.csdn.net/detail/liuyinghui523/8491405

 

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.