博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS-Swift开发基础——触控和手势
阅读量:6430 次
发布时间:2019-06-23

本文共 4850 字,大约阅读时间需要 16 分钟。

手势事件

  • UITapGestureRecognizer(点击手势)

  • UIPanGestureRecognizer(拖动手势)

  • UIPinchGestureRecognizer(缩放手势)

  • UISwipeGestureRecognizer(擦碰手势)

  • UIRotationGestureRecognizer(旋转手势)

  • UILongPressGestureRecognizer(长按手势)

添加和移除手势:

uiview.addGestureRecognizer(gesture)uiview.removeGestureRecognizer(gesture)

下面代码简单演示各个手势用法:

@IBOutlet weak var imgView: UIImageView!var lastScaleFactor: CGFloat = 1var netRotation: CGFloat = 0var netTranslation: CGPoint = CGPointMake(0,0)override func viewDidLoad() {    super.viewDidLoad()    // 点击手势    let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")    tapGesture.numberOfTapsRequired = 2  // 点击次数    self.view.addGestureRecognizer(tapGesture)    // 捏手势    let pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:")    self.view.addGestureRecognizer(pinchGesture)        // 旋转手势    let rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:")    self.view.addGestureRecognizer(rotateGesture)        // 滑动手势: 默认为右滑    let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")    swipeGesture.direction = .Down //.Left .Down .Up .Right    self.view.addGestureRecognizer(swipeGesture)    //    // 拖动手势: 与滑动手势冲突,二者选其一//    let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")//    self.view.addGestureRecognizer(panGesture)        // 长按手势    let longpressGestrue = UILongPressGestureRecognizer(target: self, action: "handleLongPressGesture:")    longpressGestrue.minimumPressDuration = 1    longpressGestrue.numberOfTouchesRequired = 1    longpressGestrue.allowableMovement = 15    self.view.addGestureRecognizer(longpressGestrue)}override func didReceiveMemoryWarning() {    super.didReceiveMemoryWarning()}// MARK: gesture methodsfunc handleTapGesture(sender: UITapGestureRecognizer) {    print("handleTapGesture")    if imgView.contentMode == UIViewContentMode.ScaleAspectFit {        imgView.contentMode = UIViewContentMode.Center    } else {        imgView.contentMode = UIViewContentMode.ScaleAspectFit    }}func handlePinchGesture(sender: UIPinchGestureRecognizer) {    print("handlePinchGesture")    let factor = sender.scale    print("factor", factor)    if factor > 1 { // 放大        imgView.transform = CGAffineTransformMakeScale(lastScaleFactor + factor - 1, lastScaleFactor + factor - 1)    } else { // 缩小        imgView.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor)    }    if sender.state == UIGestureRecognizerState.Ended {        if factor > 1 {            lastScaleFactor = lastScaleFactor + factor - 1        } else {            lastScaleFactor = lastScaleFactor * factor        }    }}func handleRotateGesture(sender: UIRotationGestureRecognizer) {    print("handleRotateGesture")    let rotation = sender.rotation    print("rotation", rotation)    imgView.transform = CGAffineTransformMakeRotation(rotation + netRotation)    if sender.state == UIGestureRecognizerState.Ended {        netRotation += rotation    }}func handleSwipeGesture(sender: UISwipeGestureRecognizer) {    print("handleSwipeGesture")}func handlePanGesture(sender: UIPanGestureRecognizer) {    print("UIPanGestureRecognizer")    let translation = sender.translationInView(imgView)    imgView.transform = CGAffineTransformMakeTranslation(netTranslation.x + translation.x, translation.y + netTranslation.y)    if sender.state == UIGestureRecognizerState.Ended {        netTranslation.x += translation.x        netTranslation.y += translation.y    }}func handleLongPressGesture(sender: UILongPressGestureRecognizer) {    print("handleLongPressGesture")    if sender.state == UIGestureRecognizerState.Began {        print("----------")    }}

状态变化:

clipboard.png

触控事件

func touchesBegan(touches: NSSet, withEvent event: UIEvent)

通知调用者当有一个或者多个手指触摸到了视图或者窗口时触发此方法

func touchesMoved(touches: NSSet, withEvent event: UIEvent)

告诉接收者一个或者多个手指在视图或者窗口上触发移动事件

func touchesEnded(touches: NSSet, withEvent event: UIEvent)

当一个触摸事件结束时发出的UITouch实例对象

func touchesCancelled(touches: NSSet, withEvent event: UIEvent)

通知接收者当系统发出取消事件的时候(如低内存消耗的告警框)

默认是不开启多点触控的,如开启:view.multipleTouchEnabled = true

override func touchesBegan(touches: Set
, withEvent event: UIEvent?) { for touch: AnyObject in touches { let tap: UITouch = touch as! UITouch print("tapCount", tap.tapCount) print("event begin!") }}override func touchesMoved(touches: Set
, withEvent event: UIEvent?) { for touch: AnyObject in touches { let tap: UITouch = touch as! UITouch print(tap.locationInView(self.view)) }}override func touchesEnded(touches: Set
, withEvent event: UIEvent?) { print("event end!")}override func touchesCancelled(touches: Set
?, withEvent event: UIEvent?) { print("event canceled!")}

参考:

转载地址:http://gdiga.baihongyu.com/

你可能感兴趣的文章
OutputCache说明
查看>>
sdl2.0示例
查看>>
数学 --- 高斯消元 POJ 1830
查看>>
Ejabberd源码解析前奏--集群
查看>>
[ZHUAN]Flask学习记录之Flask-SQLAlchemy
查看>>
【转】Install SmartGit via PPA in Ubuntu 13.10/13.04/12.04/Linux Mint
查看>>
PNG怎么转换成32位的BMP保持透明
查看>>
经验分享:CSS浮动(float,clear)通俗讲解
查看>>
WTL中最简单的实现窗口拖动的方法(转)
查看>>
数据结构—队列
查看>>
C. Adidas vs Adivon
查看>>
BZOJ4241 : 历史研究
查看>>
(LeetCode)两个队列来实现一个栈
查看>>
[WebGL入门]十九,遮挡剔除和深度測试
查看>>
jquery封装常用方法
查看>>
什么是ICE (Internet Communications Engine)
查看>>
移动web开发之屏幕三要素
查看>>
求按小时统计的语句,该怎么处理
查看>>
TRUNCATE,DORP,DELETE
查看>>
Chrome的开发必备小技巧
查看>>