91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

IOS中各種手勢操作實例代碼

發布時間:2020-09-25 19:55:04 來源:腳本之家 閱讀:218 作者:這酸爽! 欄目:移動開發

先看下效果

IOS中各種手勢操作實例代碼

手勢相關的介紹

IOS中手勢操作一般是 UIGestureRecognizer 類的幾個手勢子類去實現,一般我們用到的手勢就這么5種:

1、點擊  UITapGestureRecognizer

2、平移  UIPanGestureRecognizer

3、縮放  UIPinchGestureRecognizer

4、旋轉  UIRotationGestureRecognizer

5、輕掃  UISwipeGestureRecognizer

我們上面這個實例中就用到了上面這5種手勢,不過其中 點擊與輕掃沒有體現出來,只是輸出了下日志而已,一會看代碼

下面我們來分別介紹下這幾種手勢

1、UITapGestureRecognizer 點擊手勢

UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
// 點擊次數,默認為1,1為單擊,2為雙擊
tapGes.numberOfTapsRequired = 2;

這個點擊手勢類有一個屬性 numberOfTapsRequired 用于設置點擊數,就是點擊幾次才觸發這個事件

2、UIPanGestureRecognizer 平移手勢

// 平移手勢
- (void)initPanGes{
 UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
 [self.imgView addGestureRecognizer:panGes];
}
- (void)panGes:(UIPanGestureRecognizer*)ges{
 // 獲取平移的坐標點
 CGPoint transPoint = [ges translationInView:self.imgView];
}

平移手勢本身沒太多可設置的屬性,在平移事件觸發手,可以用  translationInView 方法獲取當前平移坐標點

3、UIPinchGestureRecognizer 縮放手勢

// 縮放手勢
- (void)initPinGes{
 UIPinchGestureRecognizer* pinGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGes:)];
 [self.imgView addGestureRecognizer:pinGes];
}
- (void)pinGes:(UIPinchGestureRecognizer*)ges{
 // 縮放
 self.imgView.transform = CGAffineTransformScale(self.imgView.transform, ges.scale, ges.scale);
}

縮放手勢在事件里面可以獲取 scale 屬性,表示當前縮放值

4、UIRotationGestureRecognizer 旋轉手勢

// 旋轉手勢
- (void)initRotation{
 UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
 [self.imgView addGestureRecognizer:rotationGes];
}
- (void)rotationGes:(UIRotationGestureRecognizer*)ges{
 // 旋轉圖片
 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation);
}

旋轉手勢在事件里面可以通過獲取 rotation 屬性獲取當前旋轉的角度

5、UISwipeGestureRecognizer 輕掃手勢

// 輕掃手勢
- (void)initSwipeGes{
 // 創建 從右向左 輕掃的手勢
 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 方向,默認是從左往右
 // 最多只能開啟一個手勢,如果要開啟多個就得創建多個手勢
 // 監聽從右向左的方向
 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft;
 [self.imgView addGestureRecognizer:swipeLeftGes];
}
- (void)swipeGes:(UISwipeGestureRecognizer*)ges{
 // ges.direction方向值
 NSLog(@"%s diection:%lu",__func__,(unsigned long)ges.direction);
}

輕掃手勢對象需要設置 direction 屬性,默認是只監聽從左向右,這是一個枚舉值 UISwipeGestureRecognizerDirection

UISwipeGestureRecognizerDirectionRight  從左向右(默認值)
UISwipeGestureRecognizerDirectionLeft   從右向左
UISwipeGestureRecognizerDirectionUp    從下向上
UISwipeGestureRecognizerDirectionDown  從上向下

下面看一下我們上面那個效果圖實現代碼吧

//
// ViewController.m
// 各種手勢操作
//
// Created by xgao on 16/3/24.
// Copyright © 2016年 xgao. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 [self initTapGes];
 [self initPanGes];
 [self initPinGes];
 [self initRotation];
 [self initSwipeGes];
}
// 點擊手勢
- (void)initTapGes{
 UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
 // 點擊次數,默認為1,1為單擊,2為雙擊
 tapGes.numberOfTapsRequired = 2;
 tapGes.delegate = self;
 [self.imgView addGestureRecognizer:tapGes];
}
- (void)tapGes:(UITapGestureRecognizer*)ges{
 NSLog(@"%s",__func__);
}
// 平移手勢
- (void)initPanGes{
 UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
 panGes.delegate = self;
 [self.imgView addGestureRecognizer:panGes];
}
- (void)panGes:(UIPanGestureRecognizer*)ges{
 // 獲取平移的坐標點
 CGPoint transPoint = [ges translationInView:self.imgView];
 // 在之前的基礎上移動圖片
 self.imgView.transform = CGAffineTransformTranslate(self.imgView.transform, transPoint.x, transPoint.y);
 // 復原,必需復原
 // 每次都清空一下消除坐標疊加
 [ges setTranslation:CGPointZero inView:self.imgView];
 NSLog(@"%s",__func__);
}
// 縮放手勢
- (void)initPinGes{
 UIPinchGestureRecognizer* pinGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGes:)];
 pinGes.delegate = self;
 [self.imgView addGestureRecognizer:pinGes];
}
- (void)pinGes:(UIPinchGestureRecognizer*)ges{
 // 縮放
 self.imgView.transform = CGAffineTransformScale(self.imgView.transform, ges.scale, ges.scale);
 // 復原
 // 每次都清空一下消除疊加
 ges.scale = 1;
}
// 旋轉手勢
- (void)initRotation{
 UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
 rotationGes.delegate = self;
 [self.imgView addGestureRecognizer:rotationGes];
}
- (void)rotationGes:(UIRotationGestureRecognizer*)ges{
 // 旋轉圖片
 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation);
 // 復原
 // 每次都清空一下消除疊加
 ges.rotation = 0;
 NSLog(@"%s",__func__);
}
// 輕掃手勢
- (void)initSwipeGes{
 // 創建 從右向左 輕掃的手勢
 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 方向,默認是從左往右
 // 最多只能開啟一個手勢,如果要開啟多個就得創建多個手勢
 // 監聽從右向左的方向
 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft;
 swipeLeftGes.delegate = self;
 [self.imgView addGestureRecognizer:swipeLeftGes];
 // 創建 從下向上 輕掃的手勢
 UISwipeGestureRecognizer* swipeUpGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 監聽從下向上的方向
 swipeUpGes.direction = UISwipeGestureRecognizerDirectionUp;
 swipeUpGes.delegate = self;
 [self.imgView addGestureRecognizer:swipeUpGes];
}
- (void)swipeGes:(UISwipeGestureRecognizer*)ges{
 // ges.direction方向值
 NSLog(@"%s diection:%lu",__func__,(unsigned long)ges.direction);
}
#pragma mark - UIGestureRecognizerDelegate
// 判斷是否能觸發手勢
- (BOOL)gestureRecognizerShouldBegin:(UITapGestureRecognizer *)gestureRecognizer{
 return YES;
}
// 是否允許多手勢操作,不是多觸摸點
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
 return YES;
}
@end

這里需要注意的有兩點:

1、對于 平移、縮放、旋轉 這3個手勢,我們如果要用它的值去處理的話,要記得復原!復原!復原!這點很重要!重要的事說3遍~~

  平移手勢里面我們需要設置 setTranslation:CGPointZero 來復原它的坐標值,不然下一次事件觸發這個坐標值會疊加
  縮放手勢里面設置 ges.scale = 1 來復原它的縮放值
  旋轉手勢里面設置 ges.rotation = 0 來復原它的角度值

2、假如我們需要多手勢一起用的時候就需要設置下delegate 里面的一個返回參數的方法了

// 是否允許多手勢操作,不是多觸摸點
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
 return YES;
}

以上所述是小編給大家介紹的IOS中各種手勢操作實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

惠东县| 大庆市| 福清市| 滦平县| 水城县| 工布江达县| 昂仁县| 南和县| 南郑县| 襄樊市| 招远市| 历史| 彩票| 洪湖市| 泾源县| 平度市| 额敏县| 合肥市| 合山市| 安国市| 乌兰浩特市| 电白县| 松桃| 双城市| 崇左市| 台江县| 大兴区| 顺昌县| 沭阳县| 庐江县| 宝清县| 贵南县| 咸阳市| 汽车| 崇明县| 建昌县| 南开区| 微博| 资溪县| 高碑店市| 敦煌市|