我的iOS 學習 - 學習基本手勢

來源:互聯網
上載者:User

標籤:

iOS設計手勢符合人的操作習慣,提供了良好的使用者體驗。

UIGestureRecognizer 手勢抽象類別,實作類別 :

  1. UITapGestureRecognizer  輕擊

  2. UILongPressGestureRecognizer  長按

  3. UISwipeGestureRecognizer  輕掃

  4. UIPanGestureRecognizer  拖動

  5. UIPinchGestureRecognizer  捏合縮放

  6. 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 學習 - 學習基本手勢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.