UItouch、手势集合、自定义图片按钮
UItouch
//MARK: - UITouch
extension ViewController{
//1.每次开始触摸的时候会自动调用
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//print("开始触摸:\(touches)")
//参数1:touches->在多个手指同时触摸屏幕的时候只能获取到一个触摸对象
//拿到当前的触摸对象
let touch = touches.first
//拿到当前触摸点的位置
//参数:计算坐标的相对视图
let location = touch?.locationInView(self.view)
print(location)
//参数2:event -> 可以拿到多个触摸对象
let allTouches = event?.allTouches()
print("ALL:\(allTouches?.count)")
//遍历拿到每个触摸对象
for item in allTouches! {
print(item.locationInView(self.view))
}
//设置球的坐标,让其出现在开始触摸的位置
self.ball.center = location!
}
//2.每次触摸结束的时候会自动调用
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("触摸结束")
}
//3.手指在屏幕上移动的时候会实时调用
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//拿到移动过程中的坐标点
let touch = touches.first
let loctaion = touch?.locationInView(self.view)
self.ball.center = loctaion!
print("移动")
}
手势集合
手势的添加步骤
//1.创建点击手势对象
//UIGestureRecognizer是所有的手势的父类
//参数1:调用方法的对象
//参数2:方法对应的选择器 -> 这个方法如果带参只能带一个参数,参数的实参就是当前创建的点击手势对象
//点击手势发生的时候让参数1调用参数2中的方法
let tap = UITapGestureRecognizer.init(target: self, action: "tapAction:")
//核心属性:点击次数(默认是1->单击)
tap.numberOfTapsRequired = 2 //双击
//2.将点击手势添加到视图上(要求:添加手势的视图必须可以进行用户交互)
//self.view.addGestureRecognizer(tap)
//3.打开图片的用户交互,并且添加点击手势
self.imageView.userInteractionEnabled = true
//注意:同一个手势对象,只能被添加到一个视图
self.imageView.addGestureRecognizer(tap)
其他几种手势名称以及核心属性
//停止UIView动画
self.imageView.layer.removeAllAnimations()
//让形变清空
self.imageView.transform = CGAffineTransformIdentity
//所有的手势类都一个状态属性,用来获取手势的开始和结束状态
longPress.state == .Began
longPress.state == .Ended
===================================
//2长按手势
longPress = UILongPressGestureRecognizer
//核心属性:长按时间
longPress.minimumPressDuration = 1
//3滑动手势
swipe = UISwipeGestureRecognizer
//核心属性:滑动方向(默认是右滑->从左往右滑)
swipe.direction = .Right
//4拖动手势
pan = UIPanGestureRecognizer
//核心属性:在指定视图上的位移
let translation = pan.translationInView(self.view)
//5缩放手势
pinch = UIPinchGestureRecognizer
//.核心属性:缩放比
let scale = pinch.scale
//6旋转手势
rotationG = UIRotationGestureRecognizer
//核心属性:旋转角度
let rotation = rotationG.rotation
创建图片按钮
建一个继承UIImageVIew的类,向BTu一样给图片添加一个事件的方法,然后创建图片的时候就可以通过这个类,就可以调用事件的方法了,就和按钮一样功能了
enum YTControlEvent {
case TouchDown //-->touchesDegan
case TouchUpInside //-->touchesEnded
}
class YTImageView: UIImageView {
//MARK: -属性
var target:AnyObject? = nil
var action:Selector? = nil
var event:YTControlEvent? = nil
//MARK: - 方法
//保存target、action和controlEvent的值
func addTarget(target: AnyObject?, action: Selector, forControlEvent controlEvent: YTControlEvent){
//打开用户交互
self.userInteractionEnabled = true
//保存对象和事件
self.target = target
self.action = action
self.event = controlEvent
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if self.event == YTControlEvent.TouchDown {
self.target?.performSelector(self.action!, withObject: self)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if self.event == YTControlEvent.TouchUpInside {
self.target?.performSelector(self.action!, withObject: self)
}
}