你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> UI中的七種手勢

UI中的七種手勢

編輯:IOS技巧綜合
[摘要]本文是對UI中的七種手勢的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
 //
 //  GestureRecognizerViewController.m

#import "GestureRecognizerViewController.h" #import "UIColor+RandomColor.h" @interface GestureRecognizerViewController () { CGRect _frame; // 用來記錄view原來的frame } @end @implementation GestureRecognizerViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // UIGestureRecognizer 手勢識別器,是所有手勢識別類的基類,提供了手勢識別器的基本功能,有了手勢識別器之後,手勢的識別全部由這個類來識別,我們就不再關心手勢識別的過程,我們只需要關心手勢識別之後應該做哪些操作,它的子類有6個,輕拍手勢,捏合手勢,長按手勢,輕掃手勢,旋轉手勢,平移手勢,以及平移手勢的子類 屏幕邊緣手勢 UIView *view = [[UIView alloc]initWithFrame:(CGRectMake(60, 184, 200, 200))]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; [view release]; //! 輕拍手勢 這種手勢用的最多, 跟按鈕似的 // UITapGestureRecognizer /* // 1. 創建輕拍手勢對象 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; // self指視圖控制器的對象 // 2. 設置輕拍觸發方法時,需要的點擊次數 // 一定要在添加手勢之前設置 tapGesture.numberOfTapsRequired = 2; tapGesture.numberOfTouchesRequired = 2; // 3. 向視圖對象上添加手勢 [view addGestureRecognizer:tapGesture]; // 多態 父類指針指向子類對象 // [view addGestureRecognizer:<#(UIGestureRecognizer *)#>] [tapGesture release]; */ /** 1. 創建手勢識別類的對象(輕拍手勢/ 長按手勢/ 輕掃手勢/ 平移手勢/ 捏合手勢/ 旋轉手勢/ 屏幕邊緣手勢) 2. 設置手勢方法的相關屬性(如需要幾根手指,多長時間才能觸發手勢事件 等) // 根據需要 3. 向視圖對象上添加手勢 4. 釋放手勢對象 */ // 長按手勢 UILongPressGestureRecognizer /* UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; // 設置長按手勢最短的觸發事件 秒為單位 longPressGesture.minimumPressDuration = 1; [view addGestureRecognizer:longPressGesture]; [longPressGesture release]; */ // 輕掃手勢 UISwipeGestureRecognizer UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; // 設置輕掃手勢支持的方法 (默認是向右掃) // UISwipeGestureRecognizerDirectionDown 向下掃 swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; // 一定要在添加手勢之前設置 [view addGestureRecognizer:swipeGesture]; [swipeGesture release]; // 一個視圖可以添加多個手勢 // 想要實現多個方向輕掃手勢: 再添加一個輕掃手勢 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; // UISwipeGestureRecognizerDirectionLeft 向左輕掃 swipe.direction = UISwipeGestureRecognizerDirectionLeft; [view addGestureRecognizer:swipe]; [swipe release]; // 平移手勢 UIPanGestureRecognizer UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesTure:)]; [view addGestureRecognizer:panGesture]; [panGesture release]; // 捏合手勢 UIPinchGestureRecognizer UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; // pinchGesture.scale [view addGestureRecognizer:pinchGesture]; [pinchGesture release]; // 旋轉手勢 // UIRotationGestureRecognizer UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)]; [view addGestureRecognizer:rotationGesture]; [rotationGesture release]; // 屏幕邊緣手勢 UIScreenEdgePanGestureRecognizer 是UIPanGestureRecognizer的子類 UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgeGesture:)]; // 設置屏幕邊緣手勢支持方法 screenEdgePanGesture.edges = UIRectEdgeRight; // 必須讓位置和屏幕邊緣重合 view.frame = CGRectMake(120, 50, 200, 200); // 使每次移動後的view回到原始位置,實現在action方法中 _frame = view.frame; [view addGestureRecognizer:screenEdgePanGesture]; [screenEdgePanGesture release]; } #pragma mark - 輕拍手勢方法 - (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { // 獲取輕拍手勢所在的視圖對象 tapGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 長按手勢方法 - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture { // UIGestureRecognizerStateBegan 當達到條件((longPressGesture.minimumPressDuration = 1;))界限時,觸發方法事件 // UIGestureRecognizerStateChanged 當達到條件時,滑動,觸發 // UIGestureRecognizerStateEnded 當達到條件,手指離開,觸發 // 根據長按手勢的狀態執行 if (longPressGesture.state == UIGestureRecognizerStateEnded) { longPressGesture.view.superview.backgroundColor = [UIColor randomColor]; } } #pragma mark - 輕掃手勢方法 - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture { swipeGesture.view.backgroundColor = [UIColor randomColor]; } - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { swipe.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 平移手勢方法 - (void)handlePanGesTure:(UIPanGestureRecognizer *)panGesture { // 1. 獲取平移增量 CGPoint point = [panGesture translationInView:panGesture.view]; // 2. 讓視圖的位置發生移動,以上次視圖為基准,transform 仿射變換技術 (view上所有點跟隨移動) 用了線性代數的知識 panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y); //3. 將上次的平移增量置為0 //CGPointZero 代表一個(0, 0)的結構體 CGPointMake(0, 0) [panGesture setTranslation:CGPointZero inView:panGesture.view]; panGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 捏合手勢方法 - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture { // 1.根據比例做放射變換, 也是以上次視圖為基准 Scale 比例 pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); // 2.將上次的縮放比例置為1 pinchGesture.scale = 1; // 或者[pinchGesture setScale:1]; //pinchGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 旋轉手勢方法 - (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture { // 1. 根據旋轉角度做放射變換,也是以上次視圖形變量為基准 rotation 旋轉,旋度 rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation); // 2. 將上次的旋轉角度清零 rotationGesture.rotation = 0; } #pragma mark - 邊緣手勢方法 // 方法和UIPanGestureRecognizer(平移手勢)一樣 - (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgeGesture { // 手指離開的時候 ,view回到原來的位置 if (screenEdgeGesture.state == UIGestureRecognizerStateEnded) { screenEdgeGesture.view.frame = _frame; } // 1.獲取手指的平移增量 CGPoint point = [screenEdgeGesture translationInView:screenEdgeGesture.view]; // 2.根據平移增量做仿射變換 screenEdgeGesture.view.transform = CGAffineTransformTranslate(screenEdgeGesture.view.transform, point.x, point.y); // 3. 把平移增量置為0的結構體 [screenEdgeGesture setTranslation:CGPointZero inView:screenEdgeGesture.view]; //screenEdgeGesture.view.frame = _frame; //NSLog(@"觸發了"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end

隨機顏色

 //
 //  UIColor+RandomColor.h
 //  UILessonTouch-04
 
 #import <UIKit/UIKit.h>
 
 @interface UIColor (RandomColor)
 + (UIColor *)randomColor;
 @end
 //
 //  UIColor+RandomColor.m
 //  UILessonTouch-04
 
 #import "UIColor+RandomColor.h"
 #define kColorValue arc4random_uniform(256) / 255.0
 @implementation UIColor (RandomColor)
 
 + (UIColor *)randomColor {
     
     return [UIColor colorWithRed:kColorValue green:kColorValue blue:kColorValue alpha:kColorValue];
     
 }
 
 @end
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved