zoukankan      html  css  js  c++  java
  • swift--触摸(UITouch)事件(点击,移动,抬起)

    触摸事件:

    UITouch:一个手机第一次点击屏幕,会形成一个UITouch对象,知道离开销毁。表示触碰。UITouch对象能表明当前手指触碰的屏幕位置、状态,状态分为开始触碰、移动、离开。

    具体方法介绍如下:

    1.override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)

    通知调用者当有一个或者多个手指触摸到了视图或者窗口时触发次方法,touches是UITouch的集合,通过uito我们可以检测触摸事件的属性,是单击还是双击,还有触摸的位置等。

    2.override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)

    告诉接受者一个或者多个手指在视图或者窗口上触发移动事件。默认不允许多点触摸,如果要接受多点触摸事件必须将UIVIew属性置为true。

    //支持多点触摸
    self.view.isMultipleTouchEnabled = true

    3.override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)

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

    4.override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)

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

    样例代码:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch:AnyObject in touches {
                let t:UITouch = touch as! UITouch
                //当在屏幕上连续拍动两下时,背景回复为白色
                if t.tapCount == 2
                {
                    self.view.backgroundColor = UIColor.white
                }else if t.tapCount == 1
                {
                    self.view.backgroundColor = UIColor.blue
                }
            }
        }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            //获取点击的坐标位置
            for touch:AnyObject in touches {
                let t:UITouch = touch as! UITouch
                print(t.location(in: self.view))
            }
        }
    override func touchesEnded(_ touches: Set<UITouch>, with 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.location(in: self.view)
                let secondPoint = second.location(in: self.view)
                //计算两点间的距离
                let deltaX = secondPoint.x - firstPoint.x
                let deltaY = secondPoint.y - firstPoint.y
                let initialDistance = sqrt(deltaX + deltaY * deltaY)
                print("两点间的距离:(initialDistance)")
                //计算两点间的角度
                let height = secondPoint.y - firstPoint.y
                let width = firstPoint.x - secondPoint.x
                let rads = atan(height/width)
                let degrees = 180.0 * Double(rads) / .pi
                print("两点间角度:(degrees)")
            }
        }
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
            print("event canceled!")
        }

  • 相关阅读:
    JS中所有数组的方法和所有的对象方法,包含ES5和ES6
    (原创!)彻底理解JS中的事件,事件处理函数,钩子函数,回调函数。
    深拷贝和浅拷贝的区别,object.assgin方法是深拷贝还是浅拷贝
    axios,Ajax,jQuery ajax,axios和fetch的区别
    python : 文档比较
    leetcode——989.数组形式的整数加法
    leetcode——66.加一
    leetcode——283.移动0
    leetcode——27.移除元素
    leetcode刷题第一天——两数之和
  • 原文地址:https://www.cnblogs.com/hero11223/p/7677665.html
Copyright © 2011-2022 走看看