关于UITouch需要了解:
所有继承自UIResponder的类(UIView和UIViewController)都可以重写UITouch的相关方法,去检测视图的开始触摸,结束触摸以及移动等触摸相关事件
触摸只有在视图可以接收用户交互的时候才可以有效,UILabel和UIImageView默认都是不能进行用户交互(设置.userInteractionEnabled属性)
在使用手势时,要拿到当前触摸的对象,可以使用.view属性,例如:
func tapAction(tap:UITapGestureRecognizer){
tag1 = tap.view?.tag//将当前触摸对象的tag值赋给tag1
}
对于UITouch还是没有什么可以讲的,放一些属性相关代码
//UITouch
extension ViewController{
//每次开始触摸的时候自动调用
//参数1:拿到当前的触摸点 touches -> 多个手指触摸的时候只能得到一个触摸对象
//参数2:withEvent event -> 可以拿到多个触摸对象
override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
print("开始")
//拿到当前的触摸对象
let touch = touches.first
//拿到当前触摸点的坐标位置
//参数:计算坐标的相对视图
let location = touch?.locationInView(self.view)
// print(location)
let alltouches = event?.allTouches()
print(alltouches?.count)
//遍历拿到每个触摸对象
for item in alltouches!{
print(item.locationInView(self.view))
}
}
//每次结束触摸自动调用
override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
print("结束")
}
//手指在屏幕上移动的时候会实时调用
override func touchesMoved(touches: Set, withEvent event: UIEvent?) {
//拿到移动过程中的坐标点
let touch = touches.first
let location = touch?.locationInView(self.view)
self.ball.center = location!
print("移动")
}
}
点击手势
import UIKit
class TapViewController: ZViewController {
override func viewDidLoad() {
super.viewDidLoad()
//创建点击对象 UIGestureRecognizer:所有手势类的父类
//参数1:点击手势发生的时候会让参数1调用参数2中的方法
//参数2:方法对应选择器(selector) -> 如果带参,只能带一个参数,参数的实参就是当前创建的点击手势对象
let tap = UITapGestureRecognizer(target: self, action: "tapAction:")
//核心属性:点击次数 numberOfTapsRequired 默认为1 单击
tap.numberOfTapsRequired = 2//双击
//将点击手势添加到视图上(要求添加手势的视图必须可以进行用户交互)
self.view.addGestureRecognizer(tap)
//打开图片的用户交互并且添加手势
self.imageView.userInteractionEnabled = true
//同一个手势只能添加到一个对象上,最后添加的有效
self.imageView.addGestureRecognizer(tap)
}
//点击的时候自动调用
func tapAction(tap:UITapGestureRecognizer){
if self.imageView.frame.size.width == 200{
self.imageView.frame = self.view.bounds
}
else{
self.imageView.frame = CGRectMake(0, 0, 200, 200)
self.imageView.center = self.view.center
}
}
}
长按手势
import UIKit
class LongViewController: ZViewController {
override func viewDidLoad() {
super.viewDidLoad()
//创建手势对象
let long = UILongPressGestureRecognizer(target: self, action: "longAction:")
self.imageView.addGestureRecognizer(long)
//核心属性:长按时间
long.minimumPressDuration = 1
}
//在长按开始和长按结束的时候都会地哦啊用 -> 一个长按手势发生,这个方法会调用两次
func longAction(long:UILongPressGestureRecognizer){
//所有的手势类都有一个属性,用来获取手势开始和结束状态
//获取开始状态
if long.state == .Began{
self.imageView.transform = CGAffineTransformMakeRotation(-0.01)
//抖动
UIView.animateWithDuration(0.07, delay: 0, options: [.Repeat,.Autoreverse], animations: { (_) in
self.imageView.transform = CGAffineTransformMakeRotation(0.01)
}, completion: nil)
}
}
override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
//让所有形变清空
self.imageView.transform = CGAffineTransformIdentity
//停止所有UIView动画
self.imageView.layer.removeAllAnimations()
}
}
滑动手势
import UIKit
class SwipeViewController: ZViewController {
override func viewDidLoad() {
super.viewDidLoad()
//创建滑动手势对象
let swipe = UISwipeGestureRecognizer(target: self, action: "swipeAction")
//添加手势
self.view.addGestureRecognizer(swipe)
//核心属性:方向,默认.Right,从左向右划
//注意:如果想要一个界面上既有左划又有右划,就添加两个方向不同的手势
swipe.direction = .Right
}
func swipeAction(){
self.navigationController?.popViewControllerAnimated(true)
}
}
拖动手势(与滑动区分:无固定方向)
import UIKit
class PanViewController: ZViewController {
//保存原始位移
var lastTransform = CGAffineTransformMakeTranslation(0, 0)
override func viewDidLoad() {
super.viewDidLoad()
//创建拖动对象
let pan = UIPanGestureRecognizer(target: self, action: "panAction:")//为了拿到手势对象,必须带参
//添加手势
self.imageView.addGestureRecognizer(pan)
}
func panAction(pan:UIPanGestureRecognizer){
//核心属性:在指定视图上的位移
let tran = pan.translationInView(self.view)
self.imageView.transform = CGAffineTransformMakeTranslation(tran.x + lastTransform.tx, tran.y + lastTransform.ty)
//不让其回到开始位置
//每次拖动结束时更新lastTransform让其形变到最后位置
if pan.state == .Ended{
self.lastTransform = self.imageView.transform
}
}
}
缩放手势
import UIKit
class PinchViewController: ZViewController {
//最初缩放比
var lastScale:CGFloat = 1
override func viewDidLoad() {
super.viewDidLoad()
//创建缩放手势对象
let pinch = UIPinchGestureRecognizer(target: self, action: "pinchAction:")//为了拿到手势对象,必须带参
//添加手势
self.imageView.addGestureRecognizer(pinch)
}
func pinchAction(pinch:UIPinchGestureRecognizer){
//核心属性:缩放比
let scale = pinch.scale
self.imageView.transform = CGAffineTransformMakeScale(scale * lastScale, scale * lastScale)
//不让其回到初始状态
if pinch.state == .Ended{
self.lastScale *= scale
}
}
}
旋转手势
class RotationViewController: ZViewController {
//初始旋转角度
var lastRotation:CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
//设置旋转手势对象
let rot = UIRotationGestureRecognizer(target: self, action: "roatationAction:")//为了拿到手势对象,必须带参
//添加手势对象
self.imageView.addGestureRecognizer(rot)
}
func roatationAction(rot:UIRotationGestureRecognizer){
//核心属性:旋转角度
let rotation = rot.rotation
self.imageView.transform = CGAffineTransformMakeRotation(rotation + lastRotation)
//不让其回到初始状态
if rot.state == .Ended{
lastRotation += rotation
}
}
}
利用UITouch制作可以点击的图片
1.写一个自己的类继承自UIImageView
import UIKit
//枚举,图片添加点击事件时选择
enum ZControlEvents{
case TouchDown // -> TouchesBegan
case TouchUpInside // -> TouchesEnded
}
class ZImageView: UIImageView {
//属性
var target:AnyObject? = nil
var action:Selector? = nil
var event:ZControlEvents? = nil
//自己为图片写的点击事件方法,只需要保存target,action,ControlEvent
func addTarget(target: AnyObject?, action: Selector, controlEvent: ZControlEvents){
//打开用户交互
self.userInteractionEnabled = true
self.target = target
self.action = action
self.event = controlEvent
}
override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
if self.event == ZControlEvents.TouchDown{
//让target调用action方法
self.target?.performSelector(self.action!, withObject: self)
}
}
override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
if self.event == ZControlEvents.TouchUpInside{
self.target?.performSelector(self.action!, withObject: self)
}
}
}
应用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//系统按钮
let btn = UIButton(frame: CGRectMake(100,100,100,50))
btn.setImage(UIImage(named: "btn_N.png"), forState: .Normal)
self.view.addSubview(btn)
//添加系统的点击事件方法
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchUpInside)
//自己的图片按钮
let imageBtn = ZImageView(frame: CGRectMake(100,300,100,100))
imageBtn.image = UIImage(named: "btn")
self.view.addSubview(imageBtn)
//添加自己为图片写的点击事件方法
imageBtn.addTarget(self, action: "imageBtnAction:", controlEvent: ZControlEvents.TouchUpInside)
}
func btnAction(){
print("系统按钮点击")
}
func imageBtnAction(btn:ZImageView){
print("image按钮点击")
}
}