最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年03月27日 15:43:51   作者:這酸爽!  
IOS中手勢操作一般是 UIGestureRecognizer 類的幾個手勢子類去實現(xiàn),一般我們用到的手勢就這么5種,具體哪幾種大家通過本文學(xué)習(xí)吧,本文重點給大家介紹IOS中各種手勢操作實例代碼,一起看看吧

先看下效果

手勢相關(guān)的介紹

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

1、點擊  UITapGestureRecognizer

2、平移  UIPanGestureRecognizer

3、縮放  UIPinchGestureRecognizer

4、旋轉(zhuǎn)  UIRotationGestureRecognizer

5、輕掃  UISwipeGestureRecognizer

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

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

1、UITapGestureRecognizer 點擊手勢

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

這個點擊手勢類有一個屬性 numberOfTapsRequired 用于設(shè)置點擊數(shù),就是點擊幾次才觸發(fā)這個事件

2、UIPanGestureRecognizer 平移手勢

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

平移手勢本身沒太多可設(shè)置的屬性,在平移事件觸發(fā)手,可以用  translationInView 方法獲取當(dāng)前平移坐標(biāo)點

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 屬性,表示當(dāng)前縮放值

4、UIRotationGestureRecognizer 旋轉(zhuǎn)手勢

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

旋轉(zhuǎn)手勢在事件里面可以通過獲取 rotation 屬性獲取當(dāng)前旋轉(zhuǎn)的角度

5、UISwipeGestureRecognizer 輕掃手勢

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

輕掃手勢對象需要設(shè)置 direction 屬性,默認(rèn)是只監(jiān)聽從左向右,這是一個枚舉值 UISwipeGestureRecognizerDirection

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

下面看一下我們上面那個效果圖實現(xiàn)代碼吧

//
// 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:)];
 // 點擊次數(shù),默認(rèn)為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{
 // 獲取平移的坐標(biāo)點
 CGPoint transPoint = [ges translationInView:self.imgView];
 // 在之前的基礎(chǔ)上移動圖片
 self.imgView.transform = CGAffineTransformTranslate(self.imgView.transform, transPoint.x, transPoint.y);
 // 復(fù)原,必需復(fù)原
 // 每次都清空一下消除坐標(biāo)疊加
 [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);
 // 復(fù)原
 // 每次都清空一下消除疊加
 ges.scale = 1;
}
// 旋轉(zhuǎn)手勢
- (void)initRotation{
 UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
 rotationGes.delegate = self;
 [self.imgView addGestureRecognizer:rotationGes];
}
- (void)rotationGes:(UIRotationGestureRecognizer*)ges{
 // 旋轉(zhuǎn)圖片
 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation);
 // 復(fù)原
 // 每次都清空一下消除疊加
 ges.rotation = 0;
 NSLog(@"%s",__func__);
}
// 輕掃手勢
- (void)initSwipeGes{
 // 創(chuàng)建 從右向左 輕掃的手勢
 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 方向,默認(rèn)是從左往右
 // 最多只能開啟一個手勢,如果要開啟多個就得創(chuàng)建多個手勢
 // 監(jiān)聽從右向左的方向
 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft;
 swipeLeftGes.delegate = self;
 [self.imgView addGestureRecognizer:swipeLeftGes];
 // 創(chuàng)建 從下向上 輕掃的手勢
 UISwipeGestureRecognizer* swipeUpGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 監(jiān)聽從下向上的方向
 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
// 判斷是否能觸發(fā)手勢
- (BOOL)gestureRecognizerShouldBegin:(UITapGestureRecognizer *)gestureRecognizer{
 return YES;
}
// 是否允許多手勢操作,不是多觸摸點
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
 return YES;
}
@end

這里需要注意的有兩點:

1、對于 平移、縮放、旋轉(zhuǎn) 這3個手勢,我們?nèi)绻盟闹等ヌ幚淼脑?,要記得?fù)原!復(fù)原!復(fù)原!這點很重要!重要的事說3遍~~

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

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

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

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

相關(guān)文章

  • 超全的iOS各種設(shè)備信息獲取方法總結(jié)(包括iPhone8/iPhone X)

    超全的iOS各種設(shè)備信息獲取方法總結(jié)(包括iPhone8/iPhone X)

    這篇文章主要給大家介紹了關(guān)于iOS各種設(shè)備信息獲取方法,iPhone8/iPhone X的后驅(qū)詳細(xì)信息也已更新,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • iOS算法教程之分段截取常數(shù)示例

    iOS算法教程之分段截取常數(shù)示例

    這篇文章主要給大家介紹了關(guān)于iOS算法教程之分段截取常數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2018-01-01
  • iOS自定義UICollectionViewFlowLayout實現(xiàn)圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現(xiàn)圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現(xiàn)圖片瀏覽效果的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • iOS 對當(dāng)前webView進行截屏的方法

    iOS 對當(dāng)前webView進行截屏的方法

    下面小編就為大家?guī)硪黄猧OS 對當(dāng)前webView進行截屏的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • ios使用NSProxy實現(xiàn)消息轉(zhuǎn)發(fā)

    ios使用NSProxy實現(xiàn)消息轉(zhuǎn)發(fā)

    本文主要介紹了ios使用NSProxy實現(xiàn)消息轉(zhuǎn)發(fā),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • iOS自定義可展示、交互的scrollView滾動條

    iOS自定義可展示、交互的scrollView滾動條

    這篇文章主要為大家詳細(xì)介紹了iOS自定義可展示、交互的scrollView滾動條,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作

    剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作

    這篇文章主要介紹了剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作,Cocos2d-x是開發(fā)游戲的利器,需要的朋友可以參考下
    2015-10-10
  • 淺談iOS 屏幕方向那點事兒

    淺談iOS 屏幕方向那點事兒

    這篇文章主要介紹了淺談iOS 屏幕方向那點事兒,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • iOS開發(fā)之App主題切換解決方案完整版(Swift版)

    iOS開發(fā)之App主題切換解決方案完整版(Swift版)

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之App主題切換完整解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • iOS實現(xiàn)PDF文件瀏覽功能

    iOS實現(xiàn)PDF文件瀏覽功能

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)PDF文件瀏覽功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04

最新評論

临泉县| 商丘市| 大同县| 武隆县| 东辽县| 肥乡县| 张家界市| 马公市| 新安县| 那坡县| 二手房| 潜山县| 沙田区| 应城市| 高淳县| 得荣县| 辽阳市| 临朐县| 佛冈县| 星座| 安庆市| 宝兴县| 从江县| 霍林郭勒市| 洞头县| 宜兰县| 金溪县| 乐陵市| 乌拉特后旗| 韩城市| 西充县| 泰安市| 石家庄市| 开化县| 浑源县| 黄石市| 塔城市| 水城县| 城市| 泰州市| 宁明县|