0

我調整了UIPanGestureRecognizer以使用下拉來消除視圖。我是這樣做的,所以現在可以將視圖拖動到y軸上,如果y的變化大於100,則視圖將被忽略。將UIView設置爲從左上角開始(0,0)

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     
      } 
     } 
} 

不過,我想視圖位置回到原來的地方,但我無法弄清楚如何..

還有,就是我的方法做這件事的好方法還是一個非常原始的方法?

+0

您是否嘗試過你的觀點的中心設置到原來的位置?如果需要,您可以在視圖控制器上引用原始中心。 –

+0

我對Swift比較陌生,所以實際上我並沒有得到什麼:/我試圖實現的是將視圖從(0,0)開始(x,y) - 左上角 – senty

+0

不是確定它是否能正常工作,但試試看: 'let frame = myView.frame' 'frame.origin = CGPointMake(0,0)' 'myView.frame = frame' –

回答

1

能不能請你對// 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 
+0

作品像魅力!非常感謝!我添加了一個'animateWithDuration'使它看起來更加自然..僅僅爲了學習,在dismissViewController中有沒有使用animateWithDuration的方法? – senty

+1

很高興知道,不知道我是否按照你的其他問題。順便說一下,雖然操縱框架的作品,我會建議檢查**自動佈局**視圖大小和位置操縱。你有沒有聽說過這件事?如果沒有,我建議檢查這個[教程](http://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2)讓自己熟悉這個概念。 –

+0

是的,我知道自動佈局,但只有這個視圖,我沒有在Storyboard上創建一個VC。非常感謝您的時間 – senty

相關問題