1. 程式人生 > >swift 中手勢的使用

swift 中手勢的使用

etx wro direction var cga 滑動事件 views float net

/**點擊手勢*/
func tapGestureDemo() {
    //建立手勢識別器
    let gesture = UITapGestureRecognizer(target: self, action: "viewTap:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
/**縮放手勢*/
func pinchGestureDemo() {
    //建立手勢識別器
    let gesture = UIPinchGestureRecognizer(target: self, action: "viewPinch:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
/**旋轉手勢*/
func rotationGestureDemo() {
    //建立手勢識別器
    let gesture = UIRotationGestureRecognizer(target: self, action: "viewRotation:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
/**滑動手勢*/
func swipeGestureDemo() {
    //建立手勢識別器
    let gesture = UISwipeGestureRecognizer(target: self, action: "viewSwipe:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
/**平移手勢*/
func panGestureDemo() {
    //建立手勢識別器
    let gesture = UIPanGestureRecognizer(target: self, action: "viewPan:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
/**長按手勢*/
func LongPressGestureDemo() {
    //建立手勢識別器
    let gesture = UILongPressGestureRecognizer(target: self, action: "viewLongPress:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
/**自定義手勢*/
func custonGestureDemo() {
    //建立手勢識別器
    let gesture = CustomGestureRecognizer(target: self, action: "viewCustom:")
    //附加識別器到視圖
    self.view1.addGestureRecognizer(gesture)
}
//-----------------------手勢action事件---------------------------
/**點擊事件*/
@IBAction func viewTap(sender: UITapGestureRecognizer) {
    print("clicked...")
}
/**縮放事件*/
func viewPinch(sender: UIPinchGestureRecognizer) {
    print("pinch...")
    let _width = view1.bounds.width
    let _height = view1.bounds.height
    view1.bounds.size = CGSize(width: _width * sender.scale, height: _height * sender.scale)
}
/**旋轉事件*/
func viewRotation(sender: UIRotationGestureRecognizer) {
    view1.transform = CGAffineTransformMakeRotation(sender.rotation)
}
/**滑動事件*/
var offsetX: CGFloat = 0
func viewSwipe(sender: UISwipeGestureRecognizer) {
    if sender.direction == UISwipeGestureRecognizerDirection.Right {
        //每次向右平移20
        offsetX += 20
        view1.transform = CGAffineTransformMakeTranslation(offsetX, 0)
    }
}
/**平移事件*/
func viewPan(sender: UIPanGestureRecognizer) {
    let _transX = sender.translationInView(view1).x
    let _transY = sender.translationInView(view1).y
    view1.transform = CGAffineTransformMakeTranslation(_transX, _transY)
}
/**長按事件*/
func viewLongPress(sender: UILongPressGestureRecognizer) {
    print("long pressed....")
}
func viewCustom(sender: CustomGestureRecognizer) {
    print("custom gesture...")
}

  

swift 中手勢的使用