手勢是行動裝置的重要互動方式。UIGestureRecognizer 可處理多種觸控行為,常見有七種。
輕拍 UITapGestureRecognizer、捏合 UIPinchGestureRecognizer、旋轉 UIRotationGestureRecognizer、平移 UIPanGestureRecognizer、螢幕邊緣輕掃 UIScreenEdgePanGestureRecognizer、長按 UILongPressGestureRecognizer、輕掃 UISwipeGestureRecognizer。
建立手勢物件並設定 target 與 action,再透過 addGestureRecognizer 加入視圖。若 UIImageView 需要接收手勢,記得開啟 userInteractionEnabled。
同一視圖可加入多種手勢。若要讓它們同時辨識,可實作 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 並回傳 YES。
在手勢回呼中,可透過 sender.view 取得作用視圖,並依需求做位移、縮放、旋轉或提示。