2015-03-25 67 views
0

如何使用Swift使用Pan Gesture Handler在兩個UIView之間滑動?在兩個UIViews之間滑動

我希望能夠向左滑動並開始顯示下一個UIView,但如果手勢未完成(到某個點),則回彈。

目前使用滑動手勢(但並不動畫,並顯示到一半預覽)

func respondToSwipeGesture(gesture: UIGestureRecognizer) { 

    if let swipeGesture = gesture as? UISwipeGestureRecognizer { 

     switch swipeGesture.direction { 
     case UISwipeGestureRecognizerDirection.Right: 
      println("Swiped right") 
      loadView1() 
     case UISwipeGestureRecognizerDirection.Left: 
      println("Swiped left") 
      loadView2() 
     default: 
      break 
     } 
    } 
} 

我怎樣才能使手勢開始顯露出未來的UIView和彈回,如果姿勢不完整?

回答

0

你應該檢查你刷卡的開始和結束位置,如果距離不夠長加載不同的看法:

var startLocation:CGPoint! 
var endLocation:CGPoint! 

if(gesture.state == .Began){ 
    startLocation = gesture.locationInView(self.view) 
} else if(gesture.state == .Ended){ 
    endLocation = gesture.locationInView(self.view) 
    var distance = endLocation.x - startLocation.x 
}