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