UItouch沸久、手勢(shì)集合谭羔、自定義圖片按鈕
UItouch
//MARK: - UITouch
extension ViewController{
//1.每次開始觸摸的時(shí)候會(huì)自動(dòng)調(diào)用
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//print("開始觸摸:\(touches)")
//參數(shù)1:touches->在多個(gè)手指同時(shí)觸摸屏幕的時(shí)候只能獲取到一個(gè)觸摸對(duì)象
//拿到當(dāng)前的觸摸對(duì)象
let touch = touches.first
//拿到當(dāng)前觸摸點(diǎn)的位置
//參數(shù):計(jì)算坐標(biāo)的相對(duì)視圖
let location = touch?.locationInView(self.view)
print(location)
//參數(shù)2:event -> 可以拿到多個(gè)觸摸對(duì)象
let allTouches = event?.allTouches()
print("ALL:\(allTouches?.count)")
//遍歷拿到每個(gè)觸摸對(duì)象
for item in allTouches! {
print(item.locationInView(self.view))
}
//設(shè)置球的坐標(biāo),讓其出現(xiàn)在開始觸摸的位置
self.ball.center = location!
}
//2.每次觸摸結(jié)束的時(shí)候會(huì)自動(dòng)調(diào)用
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("觸摸結(jié)束")
}
//3.手指在屏幕上移動(dòng)的時(shí)候會(huì)實(shí)時(shí)調(diào)用
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//拿到移動(dòng)過程中的坐標(biāo)點(diǎn)
let touch = touches.first
let loctaion = touch?.locationInView(self.view)
self.ball.center = loctaion!
print("移動(dòng)")
}
手勢(shì)集合
手勢(shì)的添加步驟
//1.創(chuàng)建點(diǎn)擊手勢(shì)對(duì)象
//UIGestureRecognizer是所有的手勢(shì)的父類
//參數(shù)1:調(diào)用方法的對(duì)象
//參數(shù)2:方法對(duì)應(yīng)的選擇器 -> 這個(gè)方法如果帶參只能帶一個(gè)參數(shù)麦向,參數(shù)的實(shí)參就是當(dāng)前創(chuàng)建的點(diǎn)擊手勢(shì)對(duì)象
//點(diǎn)擊手勢(shì)發(fā)生的時(shí)候讓參數(shù)1調(diào)用參數(shù)2中的方法
let tap = UITapGestureRecognizer.init(target: self, action: "tapAction:")
//核心屬性:點(diǎn)擊次數(shù)(默認(rèn)是1->單擊)
tap.numberOfTapsRequired = 2 //雙擊
//2.將點(diǎn)擊手勢(shì)添加到視圖上(要求:添加手勢(shì)的視圖必須可以進(jìn)行用戶交互)
//self.view.addGestureRecognizer(tap)
//3.打開圖片的用戶交互瘟裸,并且添加點(diǎn)擊手勢(shì)
self.imageView.userInteractionEnabled = true
//注意:同一個(gè)手勢(shì)對(duì)象,只能被添加到一個(gè)視圖
self.imageView.addGestureRecognizer(tap)
其他幾種手勢(shì)名稱以及核心屬性
//停止UIView動(dòng)畫
self.imageView.layer.removeAllAnimations()
//讓形變清空
self.imageView.transform = CGAffineTransformIdentity
//所有的手勢(shì)類都一個(gè)狀態(tài)屬性诵竭,用來獲取手勢(shì)的開始和結(jié)束狀態(tài)
longPress.state == .Began
longPress.state == .Ended
===================================
//2長(zhǎng)按手勢(shì)
longPress = UILongPressGestureRecognizer
//核心屬性:長(zhǎng)按時(shí)間
longPress.minimumPressDuration = 1
//3滑動(dòng)手勢(shì)
swipe = UISwipeGestureRecognizer
//核心屬性:滑動(dòng)方向(默認(rèn)是右滑->從左往右滑)
swipe.direction = .Right
//4拖動(dòng)手勢(shì)
pan = UIPanGestureRecognizer
//核心屬性:在指定視圖上的位移
let translation = pan.translationInView(self.view)
//5縮放手勢(shì)
pinch = UIPinchGestureRecognizer
//.核心屬性:縮放比
let scale = pinch.scale
//6旋轉(zhuǎn)手勢(shì)
rotationG = UIRotationGestureRecognizer
//核心屬性:旋轉(zhuǎn)角度
let rotation = rotationG.rotation
創(chuàng)建圖片按鈕
建一個(gè)繼承UIImageVIew的類话告,向BTu一樣給圖片添加一個(gè)事件的方法,然后創(chuàng)建圖片的時(shí)候就可以通過這個(gè)類卵慰,就可以調(diào)用事件的方法了沙郭,就和按鈕一樣功能了
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
//保存對(duì)象和事件
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)
}
}