引用:
在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.在viewDidLoad()方法中开启多点触控:
// 开启多点触控
self.view.multipleTouchEnabled = true;
2.重写多点触控的方法:
/**
多点触控
*/
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//开始
print("event begin!");
let t:UITouch = (touches as NSSet).allObjects[0] as! UITouch;
print(t.locationInView(self.view));
//当在屏幕上单击时,背景恢复为白色
if(t.tapCount == 1)
{
self.view.backgroundColor = UIColor.whiteColor()
}
//当在屏幕上双击时,屏幕变为红色
else if(t.tapCount == 2)
{
self.view.backgroundColor = UIColor.redColor()
}
//当在屏幕上三击时,屏幕变为黄色
else if(t.tapCount == 3)
{
self.view.backgroundColor = UIColor.yellowColor()
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//运动
print("touchesMoved");
let t:UITouch = (touches as NSSet).allObjects[0] as! UITouch;
print(t.locationInView(self.view));
print("\(touches.count)个手指滑动");
}
override func touchesEnded(touches: Set<UITouch>, 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)
print("两点间距离:\(initialDistance)")
}
//结束
print("touchesEnded");
}
缩放图片的实例:
//
// TouchViewController.swift
// HelloWorld
//
// Created by 顾杰 on 15/11/26.
// Copyright © 2015年 GuJie. All rights reserved.
//
import UIKit
class TouchViewController: UIViewController {
@IBOutlet weak var iv: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
<span style="color:#ff0000;">//开启多点触控
self.view.multipleTouchEnabled = true;</span>
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//关闭页面
@IBAction func cancelClick(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil);
}
<span style="color:#ff0000;">private var laseDistance:CGFloat = 0.0;</span>
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
<span style="color:#ff0000;">laseDistance = 0.0;</span>
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
<span style="color:#ff0000;">let t:NSSet = touches as NSSet
if(touches.count == 2){
var p1 = t.allObjects[0].locationInView(self.view);
print(p1);
var p2 = t.allObjects[1].locationInView(self.view);
print(p2);
var xx = p1.x - p2.x;
var yy = p1.y - p2.y;
var currentDistance = sqrt(xx*xx+yy*yy);
if(laseDistance == 0.0){
laseDistance = currentDistance;
}else{
if (laseDistance - currentDistance)>5 {
//缩小的操作
print("缩小");
}else if (laseDistance - currentDistance) < -5 {
//放大的操作
print("放大")
}
}
}</span>
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
}