Swift:在幻灯片手势上移动UIView

时间:2016-02-05 10:11:33

标签: ios iphone swift uiview uiviewanimation

我试图将UIView上滑动手势从其初始位置移动到固定的最终位置。图像应随手势移动,而不是独立动画。

我没有尝试任何东西,因为我不知道从哪里开始,使用哪个手势类。

enter image description here

7 个答案:

答案 0 :(得分:24)

最后就像下面那样。

let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
slideUpView.addGestureRecognizer(gesture)
slideUpView.userInteractionEnabled = true
gesture.delegate = self

当检测到手势时,会调用以下函数(此处我限制视图的最大中心为555,当视图移过此点时,我将重置为554)

func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.Began || gestureRecognizer.state == UIGestureRecognizerState.Changed {
        let translation = gestureRecognizer.translationInView(self.view)
        print(gestureRecognizer.view!.center.y)
        if(gestureRecognizer.view!.center.y < 555) {
            gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x, gestureRecognizer.view!.center.y + translation.y)
        }else {
            gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x, 554)
        }

        gestureRecognizer.setTranslation(CGPointMake(0,0), inView: self.view)
    }

}

答案 1 :(得分:10)

您可能想要使用UIPanGestureRecognizer

let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
customView.addGestureRecognizer(gesture)
gesture.delegate = self

仅沿y轴拖动对象:

func wasDragged(gesture: UIPanGestureRecognizer) {
    let translation = gesture.translationInView(self.view)

    // Use translation.y to change the position of your customView, e.g.
    customView.center.y = translation.y // Customize this.
}

答案 2 :(得分:3)

更新Swift 3.x

分配选择器时,语法已更改,现在需要#selector

let gesture = UIPanGestureRecognizer(target: self, action: #selector(navViewDragged(gesture:)))

    self.navLayoutView.addGestureRecognizer(gesture)
    self.navLayoutView.isUserInteractionEnabled = true
    gesture.delegate = self

功能实现:

func navViewDragged(gesture: UIPanGestureRecognizer){
     //Code here
}

答案 3 :(得分:3)

Swift 4:

@objc func wasDragged(_ gestureRecognizer: UIPanGestureRecognizer) {

    if gestureRecognizer.state == UIGestureRecognizer.State.began || gestureRecognizer.state == UIGestureRecognizer.State.changed {

        let translation = gestureRecognizer.translation(in: self.view)
        print(gestureRecognizer.view!.center.y)

        if(gestureRecognizer.view!.center.y < 555) {

            gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x, y: gestureRecognizer.view!.center.y + translation.y)

        }else {
            gestureRecognizer.view!.center = CGPoint(x:gestureRecognizer.view!.center.x, y:554)
        }
        gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
    }
}

拨打

let gesture = UIPanGestureRecognizer(target: self, action: self.wasDragged(gestureRecognizer:))
customView.addGestureRecognizer(gesture)
gesture.delegate = self

答案 4 :(得分:1)

在Swift 3的任何位置移动视图

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(dragged(gestureRecognizer:)))
demoView.isUserInteractionEnabled = true
demoView.addGestureRecognizer(panGesture)

功能

@objc func dragged(gestureRecognizer: UIPanGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.began || gestureRecognizer.state == UIGestureRecognizerState.changed {
        let translation = gestureRecognizer.translation(in: self.view)
        gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
        gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
    }
}

答案 5 :(得分:0)

这就是您真正喜欢Stocks应用中新闻视图的方式

首先在Storyboard中向滑动视图添加2个约束,一个约束是完全打开时的状态,另一个约束是关闭时的状态。不要忘了禁用/未安装约束之一,以便在到达场景时视图将打开或关闭。 在您的代码中引用它们

@IBOutlet weak var optionsOpenedConstraint: NSLayoutConstraint!
@IBOutlet weak var optionsVisiableConstraint: NSLayoutConstraint!

现在通过UIPanGestureRecognizer函数将viewDidLoad添加到视图中。

let gesture = UIPanGestureRecognizer(target: self, action: #selector(type(of: self).wasDragged(gestureRecognizer:)))
    optionsView.addGestureRecognizer(gesture)

最后添加此回调和2个函数:

@objc func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
    let distanceFromBottom = screenHeight - gestureRecognizer.view!.center.y
    if gestureRecognizer.state == UIGestureRecognizer.State.began || gestureRecognizer.state == UIGestureRecognizer.State.changed {
        optionsOpenedConstraint.isActive = false
        optionsVisiableConstraint.isActive = false
        let translation = gestureRecognizer.translation(in: self.view)
        if((distanceFromBottom - translation.y) < 100) {
            gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x, y: gestureRecognizer.view!.center.y + translation.y)
            gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
        }

    }
    if gestureRecognizer.state == UIGestureRecognizer.State.ended{
        if distanceFromBottom > 6{
            openOptionsPanel()
        }else{
            closeOptionsPanel()
        }
    }
}
func openOptionsPanel(){
    optionsOpenedConstraint.isActive = true
    optionsVisiableConstraint.isActive = false
    UIView.animate(withDuration: 0.5) {
        self.view.layoutIfNeeded()
    }
}

func closeOptionsPanel(){
    optionsOpenedConstraint.isActive = false
    optionsVisiableConstraint.isActive = true
    UIView.animate(withDuration: 0.5) {
        self.view.layoutIfNeeded()
    }
}

和voalá enter image description here

答案 6 :(得分:-1)

@IBAction func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {

    if MainView.bounds.contains(mainImage.frame) {
        let recognizerCenter = recognizer.location(in:  MainView)

        mainImage.center = recognizerCenter
    }

    if MainView.bounds.intersection(mainImage.frame).width > 50 && MainView.bounds.intersection(mainImage.frame).height > 0 {
        let recognizerCenter = recognizer.location(in: MainView)
        print(recognizerCenter)
        mainImage.center = recognizerCenter
    }

}

@IBAction func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) {

    mainImage.transform = mainImage.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
    recognizer.scale = 1.0
}

@IBAction func handleRotateGesture(_ recognizer: UIRotationGestureRecognizer) {

    mainImage.transform = mainImage.transform.rotated(by: recognizer.rotation)
    recognizer.rotation = 0.0

}