你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 手勢之左右滑動

IOS 手勢之左右滑動

編輯:IOS開發綜合

1. 在"ViewController.h"中增加兩個手勢property.

 

? 1 2 @property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer; @property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer; 並synthesize到"ViewController.m"文件中.

 

\

 

2.在"ViewController.m"文件中的"ViewDidLoad"方法中增加如下代碼:

 

? 1 2 3 4 5 6 7 8 self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)]; self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)]; self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:self.leftSwipeGestureRecognizer]; [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];

 

3.並在"ViewController.m"中增加如下方法;

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - (void)handleSwipes:(UISwipeGestureRecognizer *)sender { if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.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 + 100.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 = @"尼瑪的, 你在往右邊跑啊....";
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved