ios - 设置 UIView 从左上角 (0,0) 开始

标签 ios swift uigesturerecognizer viewcontroller uipangesturerecognizer

我修改了 UIPanGestureRecognizer 以使用下拉菜单关闭 View 。我是这样做的,所以现在可以在 y 轴上拖动 View ,如果 y 的变化大于 100,它会关闭 View 。

override func viewDidLoad() {
    super.viewDidLoad()

    let myPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: "myPanAction:")
    self.player.view.addGestureRecognizer(myPanGestureRecognizer)
}

func myPanAction(recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translationInView(self.view)
        if let myView = recognizer.view {
            myView.center = CGPoint(x: myView.center.x, y: myView.center.y + translation.y)
        }
        recognizer.setTranslation(CGPointZero, inView: self.view)

        if recognizer.state == UIGestureRecognizerState.Ended {
            let velocity = recognizer.velocityInView(self.view)

            if velocity.y > 100 {
                dismissViewControllerAnimated(true, completion: nil)
            } else {
                // Reposition Back                   
            }
        }
}

但是,我想将 View 放回原来的位置,但是,我不知道该怎么做......

此外,我的方法是一种很好的方法还是一种非常原始的方法?

最佳答案

能否请您尝试在 //Reposition Back 上添加注释以下代码:

let frame = myView.frame // Get the current view frame
frame.origin = CGPointZero // Change the frame origin to x:0 y:0
myView.frame = frame // Change the myView frame to frame

关于ios - 设置 UIView 从左上角 (0,0) 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34914518/

相关文章:

iOS/swift : How to implement longPressed action for backButton?

ios - 变量赋值后的 nil 值

iOS - 减少 GPUImage RAM 使用

ios - Swift 将长代码转换为短代码

ios - 将手势识别器添加到导航栏

ios - 将 ScrollView 放大到特定坐标(IPAD)

ios - 使用 AVCaptureVideoDataOutput 和 AVCaptureAudioDataOutput

ios - 为什么我的 SearchController 只匹配第一个字符?

swift - 尝试修改表情符号序列时出现问题

ios - AVPlayer 不播放 HLS 流