• Swift


    在iOS开发中,UIGestureRecognizer可以方便的响应处理手势事件。

    而如果要想更精细的处理,我们还需要借助touchesBegan,touchesMoved,touchesEnded等触摸方法。这些方法 都是UIResponder中的方法。视图控制器和视图类,都是UIResponder的子类。正是这个类,让UIView等相关触摸事件得以响应。

    具体方法介绍如下:
    1,func touchesBegan(touches: NSSet, withEvent event: UIEvent)
    通知调用者当有一个或者多个手指触摸到了视图或者窗口时触发此方法。
    touches是UITouch的集合,通过UITouch我们可以检测触摸事件的属性,是单拍还是双拍,还有触摸的位置等。

    2,func touchesMoved(touches: NSSet, withEvent event: UIEvent)
    告诉接收者一个或者多个手指在视图或者窗口上触发移动事件。
    默认不允许多点触摸。如果要接收多点触摸事件必须将UIView的属性设置为true。

    3,func touchesEnded(touches: NSSet, withEvent event: UIEvent)
    当一个触摸事件结束时发出的UITouch实例对象。

    4,func touchesCancelled(touches: NSSet, withEvent event: UIEvent)
    通知接收者当系统发出取消事件的时候(如低内存消耗的告警框)

    下面通过一个样例演示触摸事件得用法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    import UIKit
     
    class ViewController: UIViewController {
         
        override func viewDidLoad() {
            super.viewDidLoad()
             
            //支持多点触摸
            self.view.multipleTouchEnabled = true
        }
         
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
         
        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
            for touch: AnyObject in touches {
                var t:UITouch = touch as! UITouch
                //当在屏幕上连续拍动两下时,背景恢复为白色
                if(t.tapCount == 2)
                {
                    self.view.backgroundColor = UIColor.whiteColor()
                }
                    //当在屏幕上单击时,屏幕变为红色
                else if(t.tapCount == 1)
                {
                    self.view.backgroundColor = UIColor.redColor()
                }
                println("event begin!")
            }
        }
         
        override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
        {
            for touch: AnyObject in touches {
                var t:UITouch = touch as! UITouch
                println(t.locationInView(self.view))
            }
        }
         
        override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent)
        {
            //两点触摸时,计算两点间的距离
            if touches.count == 2{
                //获取触摸点
                let first = (touches as NSSet).allObjects[0] as! UITouch
                let second = (touches as NSSet).allObjects[1] as! UITouch
                //获取触摸点坐标
                let firstPoint = first.locationInView(self.view)
                let secondPoint = second.locationInView(self.view)
                //计算两点间的距离
                let deltaX = secondPoint.x - firstPoint.x
                let deltaY = secondPoint.y - firstPoint.y
                let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY)
                println("两点间距离:(initialDistance)")
            }
            println("event end!")
        }
         
        override func touchesCancelled(touches: Set<NSObject>, withEvent event: UIEvent)
        {
            println("event canceled!")
        }
    }
  • 相关阅读:
    PHP学习(6)——代码重用与函数编写的一些注意事项
    PHP学习(5)——字符串操作与POSIX正则
    PHP学习(4)——数组的使用
    Three.js基础探寻十——动画
    PHP学习(3)——数据的存储与检索
    Three.js基础探寻九——网格
    PHP学习(2)——操作符与迭代整理
    个人寒假作业项目《印象笔记》第一天
    《需求工程》阅读笔记2
    《需求工程》阅读笔记
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4838421.html
Copyright © 2020-2023  润新知