@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;
@synthesize left;
@synthesize up;
@synthesize right;
@synthesize down;
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 = @"你在往下邊滑動....";
}
}