gpt4 book ai didi

swift - 在 Swift 中添加滑动手势动画

转载 作者:行者123 更新时间:2023-11-30 14:16:20 25 4
gpt4 key购买 nike

我的应用程序中有一个滑动手势,当您滑动时,它会更改屏幕上的文本。我该如何使它看起来像是滑动到新文本而不是立即更改文本?

最佳答案

你可以做一件事!!!在你的手势 View 后面添加一个 View ,该 View 应该看起来与手势 View 相同(甚至应该有相同的文本),并且一旦识别手势后,将该 View 带到你的前面手势 View 它应该有旧的文本(不要更新文本)并更新手势 View 的文本,它移动到新添加的 View 后面,只需更改新添加的 View 的框架,以便它提供滑动效果(更改其宽度)一旦完成动画后,将该 View 返回到手势 View 并将其帧更改为之前的值。

检查此示例:

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

关于swift - 在 Swift 中添加滑动手势动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com