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

溫馨提示×

溫馨提示×

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

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

IOS 開發之swift中手勢的實例詳解

發布時間:2020-09-24 15:59:05 來源:腳本之家 閱讀:428 作者:番薯大佬 欄目:移動開發

IOS 開發之swift中手勢的實例詳解

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


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

注意:手勢效果在實施過程中,存在幾種狀態:

* Began
* Ended
* Cancelled
* Failed
* Possible

手勢效果圖

IOS 開發之swift中手勢的實例詳解

代碼示例

// 點擊
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)

// 手勢響應方法
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)

// 手勢響應方法
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 // 移動
// 手勢響應方法
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)

// 手勢響應方法
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
      }
    }
}

// 旋轉
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 rotationRecognizer = UIRotationGestureRecognizer(target: self, action: Selector("ratotionClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(rotationRecognizer)

var rotationValue:CGFloat! = 1.0 // 旋轉
// 手勢響應方法
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)

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

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

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

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

向AI問一下細節

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

AI

合江县| 梓潼县| 德清县| 镇平县| 克山县| 黔东| 特克斯县| 菏泽市| 河东区| 孟村| 阳谷县| 博客| 义马市| 包头市| 盐源县| 方山县| 宁乡县| 丹巴县| 津南区| 郧西县| 丹寨县| 札达县| 潜江市| 凌云县| 淮滨县| 高清| 福贡县| 梓潼县| 尉犁县| 渭源县| 新晃| 东兰县| 阿巴嘎旗| 南郑县| 冀州市| 邮箱| 绵竹市| 江阴市| 广河县| 浑源县| 曲靖市|