为imageView

时间:2016-01-05 20:50:13

标签: ios swift uigesturerecognizer uipangesturerecognizer

我正在使用表情符号照片应用程序,用户可以通过手机在照片上添加表情符号并在Instagram上分享等等。

用户可以将表情符号缩放,旋转和拖动到他们想要装饰照片的任何地方,我想设置边界,以便不能将表情符号拖到照片外。当前设置边界似乎有效,除非用户快速拖动表情符号(快速滑动) - 表情符号可以拖到照片外面并完全消失。我附上了handlePan的代码和应用程​​序的屏幕截图。

当用户快速拖动时,有谁可以看到表情符号为何/如何移动到界外?任何帮助将不胜感激。谢谢!

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    if(deleteMode) {
        return
    }

    let translation = recognizer.translationInView(self.view)
    var centerX: CGFloat!
    var centerY: CGFloat!
    var ipadLeftBoundary: CGFloat!

    if let view = recognizer.view {

        if(is_ipad) {
            ipadLeftBoundary = 55
        } else {
            ipadLeftBoundary = 0
        }

        print("panelBackground.frame.origin.x: \(panelBackground.frame.origin.x)")
        print("view.center.x: \(view.center.x)")
        print("panelBackground.frame.origin.y: \(panelBackground.frame.origin.y)")
        print("view.center.y: \(view.center.y)")
        print("panelBackground width: \(panelBackground.frame.size.width)")

        // setting boundary for x
        if(view.center.x + translation.x < panelBackground.frame.origin.x - ipadLeftBoundary) {
            centerX = view.center.x + translation.x + 20
        } else if(view.center.x > panelBackground.frame.size.width){
            centerX = view.center.x + translation.x - 20
        } else {
            centerX = view.center.x + translation.x
        }
        // setting boundary for y
        if(view.center.y + 20 < panelBackground.frame.origin.y - 60){
            centerY = view.center.y + translation.y + 20
        } else if(view.center.y > panelBackground.frame.size.height){
            centerY = view.center.y + translation.y - 20
        } else {
            centerY = view.center.y + translation.y
        }

        // set final position
        view.center = CGPoint(x:centerX, y:centerY)
        recognizer.setTranslation(CGPointZero, inView: self.view)

    }
}

screenshot of the app

0 个答案:

没有答案