IOS-滑动手势添加

IOS-滑动手势添加


1.新建一个Single View Application项目,选下一步,项目命名为swipeGestureTest

技术分享


技术分享


2.修改ViewController类文件

(1)在ViewController.h文件中,添加属性

@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)在ViewController.m文件中,添加代码

@synthesize left;

@synthesize up;

@synthesize right;

@synthesize down;


(3)- (void)viewDidLoad函数中,添加代码

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

self.left.direction=UISwipeGestureRecognizerDirectionLeft;

[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)添加手势响应函数

- (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 = @"你在往左边滑动....";

}

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 = @"你在往右边滑动....";

}

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 = @"你在往上边滑动....";

}

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 = @"你在往下边滑动....";

}

}

3 运行

技术分享
源码链接:http://download.csdn.net/detail/liuyinghui523/8491405


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。