欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS 開發(fā)之swift中手勢的實例詳解

 更新時間:2017年09月12日 10:14:02   作者:番薯大佬  
這篇文章主要介紹了IOS 開發(fā)之swift中手勢的實例詳解的相關(guān)資料,希望通過本文大家能掌握IOS手勢的使用方法,需要的朋友可以參考下

IOS 開發(fā)之swift中手勢的實例詳解

手勢操作主要包括如下幾類


手勢 屬性 說明
點擊 UITapGestureRecognizer numberOfTapsRequired:點擊的次數(shù);numberOfTouchesRequired:點擊時有手指數(shù)量 設(shè)置屬性 numberOfTapsRequired 可以實現(xiàn)單擊,或雙擊的效果
滑動 UISwipeGestureRecognizer direction:滑動方向 direction 滑動方向分為上Up、下Down、左Left、右Right
拖動 UIPanGestureRecognizer 在拖動過程中,通過方法 translationInView 獲取拖動時的位移
長按 UILongPressGestureRecognizer minimumPressDuration:長按最少時間
旋轉(zhuǎn) UIRotationGestureRecognizer
縮放 UIPinchGestureRecognizer

注意:手勢效果在實施過程中,存在幾種狀態(tài):

* Began
* Ended
* Cancelled
* Failed
* Possible

手勢效果圖

代碼示例

// 點擊
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-單指單擊手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapClick:"))
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
label.userInteractionEnabled = true
label.addGestureRecognizer(tapRecognizer)

// 手勢響應(yīng)方法
func tapClick(recognizer:UITapGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel
    label.textColor = UIColor.redColor()
}

// 滑動
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-左滑手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipeLeftClick:"))
swipeLeftRecognizer.direction = .Left
swipeLeftRecognizer.numberOfTouchesRequired = 1
label.userInteractionEnabled = true
label.addGestureRecognizer(swipeLeftRecognizer)

// 手勢響應(yīng)方法
func swipeLeftClick(recognizer:UISwipeGestureRecognizer)
{
    let label = recognizer.view
    label!.backgroundColor = UIColor.orangeColor()
}

// 拖動
let label = UILabel(frame: CGRectMake(10.0, 10.0, 100.0, 100.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-拖動手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let panRecognizer = UIPanGestureRecognizer(target: self, action: Selector("panClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(panRecognizer)

var pointValue:CGPoint! = CGPointZero // 移動
// 手勢響應(yīng)方法
func panClick(recognizer:UIPanGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    let point = recognizer.translationInView(label)
    print("pan point = \(point)")

    // 移動
    label.transform = CGAffineTransformMakeTranslation(point.x + self.pointValue.x, point.y + self.pointValue.y)

    if recognizer.state == .Began
    {
      label.backgroundColor = UIColor.yellowColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()

      self.pointValue.x += point.x
      self.pointValue.y += point.y
    }
}

// 縮放
let label = UILabel(frame: CGRectMake(10.0, 10.0, 200.0, 200.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-捏合手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinchClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(pinchRecognizer)

// 手勢響應(yīng)方法
var scaleValue:CGFloat! = 1.0 // 縮放
func pinchClick(recognizer:UIPinchGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    let scale = recognizer.scale;
    if scale > 1.0
    {
      // 放大
      label.transform = CGAffineTransformMakeScale(self.scaleValue + scale - 1.0, self.scaleValue + scale - 1.0)
    }
    else
    {
      // 縮小
      label.transform = CGAffineTransformMakeScale(self.scaleValue * scale, self.scaleValue * scale)
    }

    if recognizer.state == .Began
    {
      label.backgroundColor = UIColor.greenColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()

      if scale > 1.0
      {
        self.scaleValue = self.scaleValue + scale - 1.0;
      }
      else
      {
        self.scaleValue = self.scaleValue * scale
      }
    }
}

// 旋轉(zhuǎn)
let label = UILabel(frame: CGRectMake(10.0, 10.0, 200.0, 200.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-旋轉(zhuǎn)手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let rotationRecognizer = UIRotationGestureRecognizer(target: self, action: Selector("ratotionClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(rotationRecognizer)

var rotationValue:CGFloat! = 1.0 // 旋轉(zhuǎn)
// 手勢響應(yīng)方法
func ratotionClick(recognizer:UIRotationGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    let rotation = recognizer.rotation
    label.transform = CGAffineTransformMakeRotation(rotation + self.rotationValue)

    if recognizer.state == .Began
    {
      label.backgroundColor = UIColor.greenColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()

      self.rotationValue = self.rotationValue + rotation
    }
}

// 長按
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-長按手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let pressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("pressClick:"))
    pressRecognizer.minimumPressDuration = 3.0
    label.userInteractionEnabled = true
    label.addGestureRecognizer(pressRecognizer)

// 手勢響應(yīng)方法
func pressClick(recognizer:UILongPressGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    if recognizer.state == .Began
    {
      let alertView = UIAlertView(title: nil, message: "長按響應(yīng)", delegate: nil, cancelButtonTitle: "知道了")
      alertView.show()

      label.backgroundColor = UIColor.orangeColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()
    }
}

 如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • iOS復(fù)數(shù)cell下優(yōu)雅的代碼結(jié)構(gòu)詳解

    iOS復(fù)數(shù)cell下優(yōu)雅的代碼結(jié)構(gòu)詳解

    這篇文章主要給大家介紹了關(guān)于iOS復(fù)數(shù)cell下優(yōu)雅的代碼結(jié)構(gòu)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • iOS開發(fā)中多線程的安全隱患總結(jié)

    iOS開發(fā)中多線程的安全隱患總結(jié)

    在本篇文章中小編給大家整理了關(guān)于iOS開發(fā)中多線程的安全隱患的知識點,需要的朋友們學(xué)習(xí)參考下。
    2019-07-07
  • iOS基礎(chǔ)知識之@property 和 Ivar 的區(qū)別

    iOS基礎(chǔ)知識之@property 和 Ivar 的區(qū)別

    這篇文章主要介紹了iOS基礎(chǔ)知識之@property 和 Ivar 的區(qū)別介紹,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • iOS當(dāng)多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解

    iOS當(dāng)多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解

    在多線程中,有時候我們會遇到一個界面同時有多個網(wǎng)絡(luò)請求(比如a,b,c,d四個網(wǎng)絡(luò)請求),在這四個個請求結(jié)束后,在請求到數(shù)據(jù)去做其他操作(UI更新等),下面這篇文章主要給大家介紹了關(guān)于iOS當(dāng)多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • iOS判斷運營商類型的實現(xiàn)方法

    iOS判斷運營商類型的實現(xiàn)方法

    下面小編就為大家?guī)硪黄猧OS判斷運營商類型的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS實現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面

    iOS實現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面

    這篇文章主要給大家介紹了關(guān)于iOS實現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來看看吧。
    2017-06-06
  • iOS Moya實現(xiàn)OAuth請求的方法

    iOS Moya實現(xiàn)OAuth請求的方法

    這篇文章主要介紹了iOS Moya實現(xiàn)OAuth請求的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 舉例講解設(shè)計模式中的原型模式在iOS應(yīng)用開發(fā)中的作用

    舉例講解設(shè)計模式中的原型模式在iOS應(yīng)用開發(fā)中的作用

    這篇文章主要介紹了設(shè)計模式中的原型模式在iOS應(yīng)用開發(fā)中的作用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-04-04
  • ios12中遇到的帶input彈窗的錯位問題的解決方法

    ios12中遇到的帶input彈窗的錯位問題的解決方法

    這篇文章主要介紹了ios12中遇到的帶input彈窗的錯位問題的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • iOS自定義View實現(xiàn)卡片滑動

    iOS自定義View實現(xiàn)卡片滑動

    這篇文章主要為大家詳細(xì)介紹了ios自定義View實現(xiàn)卡片滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02

最新評論