添加动画以在Swift中滑动手势

时间:2015-06-30 06:51:10

标签: swift uiswipegesturerecognizer

我的应用程序中有一个滑动手势,当您滑动时,它会更改屏幕上的文本。我怎么做才能看到它滑动到新文本而不是立即改变文本?

1 个答案:

答案 0 :(得分:0)

你可以做一件事!!!在你的手势视图后面添加一个视图,该视图应该与手势视图相同(甚至应该有相同的文本),并且一旦识别出手势后将该视图带到你的前面手势视图它应该有旧文本(不要更新文本)并更新你在新添加的视图后面移动的手势视图文本,只需更改新添加视图的框架,使其呈现滑动类型完成动画后的效果(更改其宽度)一次将该视图恢复到手势视图并将其帧更改为之前的值。

检查此样本:

 func handleGesture(sender:UISwipeGestureRecognizer) {
    //bring background view to front
    self.view.bringSubviewToFront(self.backGroundView)
    self.gestureView.userInteractionEnabled = false

    //capture its initialAutolayout constraint
    let backgroundViewSpaceConstarint  = self.backGroundViewLeftSpaceConstraint.constant
    UIView.animateWithDuration(2, animations: { [unowned self]() -> Void in
        self.someRandomValue++

        // update text label in gesture view which is behind background View
        self.gestureViewTextLabel.text = "swiped \(self.someRandomValue) times"

        //slide backgroundView in required direction by using autolayout
        self.backGroundViewLeftSpaceConstraint.constant = self.backGroundView.bounds.size.width
        self.backGroundView.layoutIfNeeded()
        }) { [unowned self](completion:Bool) -> Void in
            //after animation complition bring gesture view to the front.
            self.backgroundTextLabel.text =  "swiped \(self.someRandomValue) times"
            self.gestureView.userInteractionEnabled = true

            //upadte background view so that it will look the same for next swipe
            self.backGroundViewLeftSpaceConstraint.constant = backgroundViewSpaceConstarint
            //send the background view behind gesture view
            self.view.sendSubviewToBack(self.backGroundView)
          }
  }