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