iOS——常用的手势总结
生活随笔
收集整理的這篇文章主要介紹了
iOS——常用的手势总结
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
iOS——常用的手勢總結(jié)
前言
我們在平時玩手機(jī)的時候都知道關(guān)于手勢的重要性,在加入手勢的應(yīng)用時,使我們的App變得更加方便使用,那么具體iOS有幾種常用的手勢呢?下面我簡單的總結(jié)一下那些關(guān)于iOS最常用的手勢。關(guān)于手勢的應(yīng)用iOS提供了UIGestureRecognizer類。手勢識別UIGestureRecognizer類是個抽象類,下面的子類是具體的手勢,開發(fā)這可以直接使用這些手勢識別:
使用手勢的步驟
使用手勢很簡單,分為兩步:
需要注意的是:一個手勢只能對應(yīng)一個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];它 的回調(diào)方法:
- (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(縮放手勢)
//創(chuàng)建手勢對象 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichGestureClick:)];//添加到視圖 [imageView addGestureRecognizer:pinch];關(guān)聯(lián)方法:
- (void)pichClick:(UIPinchGestureRecognizer *)pinch {//縮放的系數(shù)NSLog(@"%.2lf", pinch.scale);//固定寫法pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);//重置縮放系數(shù)(否則系數(shù)會累加)pinch.scale = 1.0; }UIRotationGestureRecognizer(旋轉(zhuǎn)手勢)
//創(chuàng)建手勢對象 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureClick:)];//添加到視圖 [imageView addGestureRecognizer:rotation];關(guān)聯(lián)方法:
- (void)rotationClick:(UIRotationGestureRecognizer *)rotation {//rotation.rotation 手勢旋轉(zhuǎn)的角度rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);//重置角度rotation.rotation = 0; }UITapGestureRecognizer (點按手勢)
//創(chuàng)建手勢對象 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];//設(shè)置相關(guān)屬性 //點擊次數(shù)(默認(rèn)1) tap.numberOfTapsRequired = 1; //手指的個數(shù)(默認(rèn)1) tap.numberOfTouchesRequired = 1; //添加到視圖 [testView addGestureRecognizer:tap];關(guān)聯(lián)方法:
- (void)tapClick:(UITapGestureRecognizer *)tap{NSLog(@"點按手勢響應(yīng)!"); }添加兩個ImagView并添加手勢
前面說過一個手勢只能對應(yīng)一個View,但是一個View可以有多個手勢,那么如何將兩個View都設(shè)定同樣的手勢呢?
- (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]]; }總結(jié)
以上是生活随笔為你收集整理的iOS——常用的手势总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这届毕业生薪资高,是真的
- 下一篇: 初识Buildroot