8.18 手勢(shì)識(shí)別器?UIGestureRecognizer

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

//         手勢(shì)識(shí)別器
//        UIGestureRecognizer
//        識(shí)別在某一個(gè)視圖上的操作
//        瞬間觸發(fā):作用時(shí)間短,位移相對(duì)小,一般只會(huì)觸發(fā)一次
//        tap/swipe
//        持續(xù)觸發(fā):作用時(shí)間長(zhǎng),位移相對(duì)大,會(huì)定時(shí)或相隔一段距離觸發(fā)
//        pinch/rotato/long press/pan
//        點(diǎn)擊

        let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
        self.view.addGestureRecognizer(tap)
        
//        pinch 捏合

        let up = UIPinchGestureRecognizer(target: self, action: #selector(didUp(_:)))
        self.view.addGestureRecognizer(up)
        
        let se = UIScreenEdgePanGestureRecognizer(target: self, action:#selector(didSe(_:)))
        self.view.addGestureRecognizer(se)
        
        let leftSwipe = UISwipeGestureRecognizer(target: self, action:#selector(didSwipe(_:)))
        leftSwipe.direction = .Left    //滑向某個(gè)方向
        self.view.addGestureRecognizer(leftSwipe)
    }
    
    
    func didSwipe(sender: UISwipeGestureRecognizer){
        print("left")
    }

    func didTap(sender: UITapGestureRecognizer) {
        //返回值是參數(shù)坐標(biāo)
        let location = sender.locationInView(self.view)
        print("tap:\(location)")

        //UIAlertView + UIActionSheet
        let alertCtrl = UIAlertController(title: "警告", message: "不要亂點(diǎn)", preferredStyle: .Alert) //如果使用.ActionSheet,提示欄從下方彈上來(lái)
        
        let action01 = UIAlertAction(title: "OK", style: .Default) {
            (action) in
            print("OK")
        }
        
        let action02 = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
            print("Cancel")
        }
        
        let action03 = UIAlertAction(title: "3", style: .Default) { (action) in
            print("3")
        }
        
        alertCtrl.addAction(action01)
        alertCtrl.addAction(action02)
        alertCtrl.addAction(action03)
      
        self.presentViewController(alertCtrl, animated: true, completion: nil)
    }

//    捏合
    func didUp(sender: UIPinchGestureRecognizer) {
        let actionC1 = UIAlertController(title: "hehe", message: "HAHA", preferredStyle: .Alert)
        let action01 = UIAlertAction(title: "OK", style: .Default) {
            (action) in
            print("OK")

        }
    
        actionC1.addAction(action01)
        self.presentViewController(actionC1, animated: true, completion: nil)
    }
    
    func didSe(sender: UIPinchGestureRecognizer) {
        let actionC2 = UIAlertController(title: "???", message: "!!!", preferredStyle: .Alert)
        let action02 = UIAlertAction(title: "OK", style: .Default) {
            (action) in
            print("OK")
        }
        actionC2.addAction(action02)
        self.presentViewController(actionC2, animated: true, completion: nil)
    }
}

// * xxxView.transform = CGaffineTransformMakeRotation(CGFloat(M_PI_2)旋轉(zhuǎn)某視圖
//HUD控件自己研究,非常實(shí)用,但是要注意“時(shí)機(jī)”

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容