日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS——常用的手势总结

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS——常用的手势总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iOS——常用的手勢總結

前言

我們在平時玩手機的時候都知道關于手勢的重要性,在加入手勢的應用時,使我們的App變得更加方便使用,那么具體iOS有幾種常用的手勢呢?下面我簡單的總結一下那些關于iOS最常用的手勢。關于手勢的應用iOS提供了UIGestureRecognizer類。手勢識別UIGestureRecognizer類是個抽象類,下面的子類是具體的手勢,開發這可以直接使用這些手勢識別:

使用手勢的步驟

使用手勢很簡單,分為兩步:

  • 創建手勢實例。當創建手勢時,指定一個回調方法,當手勢開始,改變、或結束時,回調方法被調用。
  • 添加到需要識別的View中。每個手勢只對應一個View,當屏幕觸摸在View的邊界內時,如果手勢和預定的一樣,那就會回調方法。
  • 需要注意的是:一個手勢只能對應一個View,但是一個View可以有多個手勢。

    UIPanGestureRecognizer (拖動手勢)

    新建一個ImageView,然后添加手勢

    UIImageView *panImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pan.png"]]; panImageView.frame = CGRectMake(50, 50, 100, 160); UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [panImageView addGestureRecognizer:panGestureRecognizer]; [self.view setBackgroundColor:[UIColor whiteColor]]; [self.view addSubview:panImageView];

    它 的回調方法:

    - (void) handlePan:(UIPanGestureRecognizer*) recognizer { CGPoint translation = [recognizer translationInView:self.view]; recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); [recognizer setTranslation:CGPointZero inView:self.view]; }

    UIPinchGestureRecognizer(縮放手勢)

    //創建手勢對象 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichGestureClick:)];//添加到視圖 [imageView addGestureRecognizer:pinch];

    關聯方法:

    - (void)pichClick:(UIPinchGestureRecognizer *)pinch {//縮放的系數NSLog(@"%.2lf", pinch.scale);//固定寫法pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);//重置縮放系數(否則系數會累加)pinch.scale = 1.0; }

    UIRotationGestureRecognizer(旋轉手勢)

    //創建手勢對象 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureClick:)];//添加到視圖 [imageView addGestureRecognizer:rotation];

    關聯方法:

    - (void)rotationClick:(UIRotationGestureRecognizer *)rotation {//rotation.rotation 手勢旋轉的角度rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);//重置角度rotation.rotation = 0; }

    UITapGestureRecognizer (點按手勢)

    //創建手勢對象 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];//設置相關屬性 //點擊次數(默認1) tap.numberOfTapsRequired = 1; //手指的個數(默認1) tap.numberOfTouchesRequired = 1; //添加到視圖 [testView addGestureRecognizer:tap];

    關聯方法:

    - (void)tapClick:(UITapGestureRecognizer *)tap{NSLog(@"點按手勢響應!"); }

    添加兩個ImagView并添加手勢

    前面說過一個手勢只能對應一個View,但是一個View可以有多個手勢,那么如何將兩個View都設定同樣的手勢呢?

    - (void)viewDidLoad { [super viewDidLoad]; UIImageView *image1ImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]]; UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2.png"]]; snakeImageView.frame = CGRectMake(120, 120, 100, 160); dragonImageView.frame = CGRectMake(50, 50, 100, 160); [self.view addSubview:image1ImageView]; [self.view addSubview:image2ImageView]; for (UIView *view in self.view.subviews) { UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)]; [view addGestureRecognizer:panGestureRecognizer]; [view addGestureRecognizer:pinchGestureRecognizer]; [view addGestureRecognizer:rotateRecognizer]; [view setUserInteractionEnabled:YES]; } [self.view setBackgroundColor:[UIColor whiteColor]]; }

    總結

    以上是生活随笔為你收集整理的iOS——常用的手势总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。