在两个UIViews之间滑动

时间:2015-03-25 05:04:18

标签: objective-c swift uigesturerecognizer

如何使用Pan Gesture Handler和Swift在两个UIViews之间滑动?

我希望能够向左滑动并开始显示下一个UIView,但如果手势没有完成(到某一点),则会重新拍摄。

目前正在使用Swipe Gesture(但它没有动画并在中途显示预览)

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并快照回来?

1 个答案:

答案 0 :(得分: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
}