iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 7
0
自我挑戰組

白鬍子老頭30天 Swift Ios系列 第 10

Day(7+3) lecture 5 -2 Gesture

  • 分享至 

  • xImage
  •  

有天忙太晚所以我的鐵人卡在7天了
無法當鐵人了已哭

Gesture

來源: http://furnacedigital.blogspot.tw/2012/06/uigesturerecognizer.html

//點擊動作手勢辨識
UITapGestureRecognizer;
//掐擠動作的手勢辨識
UIPinchGestureRecognizer;
//旋轉動作的手勢辨識
UIRotationGestureRecognizer;
//滑動動作的手勢辨識
UISwipeGestureRecognizer;
//拖曳動作的手勢辨識
UIPanGestureRecognizer;
//久按動作的手勢辨識
UILongPressGestureRecognizer;

使用方法

利用didSet
action 你要執行的方法當手勢成功觸碰
self 你要提醒的目標

 @IBOutlet var faceView: FaceView! {
        didSet{
            let recognizer = UIPanGestureRecognizer(target: self, action: #selector(FaceView.SomeFunc(_:)))
            faceView.addGestureRecognizer(recognizer)
        }
    }

另一種是直接從storyboard拉出來使用在hook到controller裡
http://ithelp.ithome.com.tw/upload/images/20161225/20103833rsaJVGComs.png

這裡新增了手勢控制臉部表情
嘴巴控制微笑
這裡加入兩個手勢分別是上跟下

   let happierSwapRecognizer = UISwipeGestureRecognizer (target: self, action: #selector(FaceViewController.IncreaseHappy(_:)))
            happierSwapRecognizer.direction = .up
            faceView.addGestureRecognizer(happierSwapRecognizer)
            
            let saderSwapRecognizer = UISwipeGestureRecognizer (target: self, action: #selector(FaceViewController.IncreaseSad(_:)))
            saderSwapRecognizer.direction = .down
            faceView.addGestureRecognizer(saderSwapRecognizer)

成功觸發則會呼叫這兩個func控制嘴巴

func IncreaseHappy(_ recognizer : UISwipeGestureRecognizer) {
        expression.mouth = FacialExpression.MouthSmile.Happy
        print("up")
    }
    func IncreaseSad(_ recognizer : UISwipeGestureRecognizer) {
        expression.mouth = FacialExpression.MouthSmile.Sad
        print("down")
    }

再來又加了縮放功能

faceView.addGestureRecognizer( UIPinchGestureRecognizer(target: self.faceView, action: #selector(FaceView.ChangeScale(_:))))

一樣成功會呼叫FaceView裡的縮放
這邊scale會重置變1好讓縮放比例等量前進

 
    public func ChangeScale(_ recognizer : UIPinchGestureRecognizer){
        switch recognizer.state {
        case .changed :
            scale *= recognizer.scale
            print("scaling...\(scale)")
            recognizer.scale = 1.0
        default:
            break
        }
    }

最後是點按手勢控制眼睛閉合
這是從StoryBoard直接拉的

    @IBAction func eyeToggle(_ sender: UITapGestureRecognizer) {
        if  sender.state == .ended{
            switch expression.eye {
            case .Open:
                expression.eye = .Closed
                
            default:
                expression.eye = .Open
                
            }
        }
    }

上一篇
Day(7+2) lecture 5 -1 FaceView MVC
下一篇
Day(7+4) Lecture 6 Seuges
系列文
白鬍子老頭30天 Swift Ios30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言