標籤:
iOS設計手勢符合人的操作習慣,提供了良好的使用者體驗。
UIGestureRecognizer 手勢抽象類別,實作類別 :
UITapGestureRecognizer 輕擊
UILongPressGestureRecognizer 長按
UISwipeGestureRecognizer 輕掃
UIPanGestureRecognizer 拖動
UIPinchGestureRecognizer 捏合縮放
UIRotationGestureRecognizer 旋轉
下面是樣本,簡單的建立一個view,測試使用手勢
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 5 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 300, 300) ]; 6 view.backgroundColor = [UIColor greenColor]; 7 8 /** tap 輕擊手勢 **/ 9 /**10 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundByTap:)]; //tap 手勢11 tapGesture.numberOfTapsRequired = 2; // tap觸發次數12 tapGesture.numberOfTouchesRequired = 2; // tap手指數13 14 [view addGestureRecognizer:tapGesture]; //view 增加手勢15 **/16 17 /** longPress 長按手勢 **/18 /**19 UILongPressGestureRecognizer *longpressGresture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundByLongPress:)];20 21 [view addGestureRecognizer:longpressGresture];22 **/23 24 /** swipe 親掃手勢 **/25 /**26 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundBySwipe:)];27 swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; // 輕掃方向28 29 [view addGestureRecognizer:swipeGesture];30 **/31 32 /** pan 拖動手勢 **/33 /**34 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];35 36 [view addGestureRecognizer:panGesture];37 **/38 39 /** pinch 縮放手勢 **/40 /**41 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];42 43 [view addGestureRecognizer:pinchGesture];44 **/45 46 /** rotation 旋轉手勢 **/47 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];48 49 [view addGestureRecognizer:rotationGesture];50 51 [self.view addSubview: view];52 }
手勢對應 action
1 #pragma mark - 2 #pragma mark tap 輕擊手勢 3 - (void)changeBackgroundByTap:(UITapGestureRecognizer *)tapGesture { 4 UIView *view = tapGesture.view; //取得手勢作用的view視圖 5 view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0]; 6 } 7 8 #pragma mark - 9 #pragma mark longPress 長按手勢10 - (void) changeBackgroundByLongPress:(UILongPressGestureRecognizer *)longPressGesture {11 UIView *view = longPressGesture.view;12 13 // 這裡判斷狀態,不然會調用兩次14 if (longPressGesture.state == UIGestureRecognizerStateBegan) {15 view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];16 }17 }18 19 #pragma mark -20 #pragma mark swipe 撥動手勢21 - (void) changeBackgroundBySwipe:(UISwipeGestureRecognizer *)swipeGesture {22 UIView *view = swipeGesture.view;23 view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];24 }25 26 #pragma mark -27 #pragma mark pan 拖動手勢28 - (void) panAction:(UIPanGestureRecognizer *)panGesture {29 UIView *view = panGesture.view;30 CGPoint offset = [panGesture translationInView:view];31 NSLog(@"pan 位移 = %@", NSStringFromCGPoint(offset));32 33 view.transform = CGAffineTransformMakeTranslation(offset.x, offset.y); // 設定transfrom實現手勢拖動,34 }35 36 #pragma mark -37 #pragma mark pinch 縮放手勢38 - (void) pinchAction:(UIPinchGestureRecognizer *)pinchGesture {39 CGFloat pinchScale = pinchGesture.scale;40 NSLog(@"pinchSale 縮放比例 = %f", pinchScale);41 42 pinchGesture.view.transform = CGAffineTransformMakeScale(pinchScale, pinchScale); //設定transform實現手勢縮放43 }44 45 #pragma mark -46 #pragma mark rotation 旋轉手勢47 - (void) rotationAction:(UIRotationGestureRecognizer *)rotationGesture {48 CGFloat rotation = rotationGesture.rotation; // 旋轉弧度49 NSLog(@"rotation 弧度 = %f", rotation);50 51 rotationGesture.view.transform = CGAffineTransformMakeRotation(rotation); //設定transform實現手勢旋轉52 }
以上是iOS手勢的基本操作,視圖的transform涉及到動畫的知識,還在學習中。。。
我的iOS 學習 - 學習基本手勢