gpt4 book ai didi

ios - UIView.animateWithDuration 或 UIScrollView.animateWithDuration - 如何在当前状态暂停

转载 作者:搜寻专家 更新时间:2023-11-01 06:47:52 32 4
gpt4 key购买 nike

我正在使用这些(代码块 1 和 2)将带有动画的 UIScrollView 从开始移动到我的结束点(x:0 到 1000,在这个例子中)

代码块 1:

UIScrollView.animateWithDuration(Double(5), delay: 1, options: (UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.CurveLinear | UIViewAnimationOptions.BeginFromCurrentState),
animations: {
self.myScrollView.contentOffset = CGPointMake(1000, 0)
},
completion: {
finished in
if(finished)
{

}
})

代码块 2:

UIView.animateWithDuration ...

问题是不能在当前x点暂停。

我已经尝试过了,但对我不起作用。

  • 导入 QuartzCore 框架
  • self.myScrollView.layer.beginTime = 0.0 完成
  • CATransaction.begin() 和 CATransaction.commit()

  • self.myScrollView.layer.removeAllAnimations()。这将完全停止动画,然后 UIScrollView 将到达终点(到 x:1000)

同时 animateWithDuration 正在取消 scrollViewDidScroll,因此无法获取当前位置。另外,同样的原因无法在Pause函数中获取到当前位置。

func scrollViewDidScroll(scrollView: UIScrollView)
{
scrollView.bounds.origin.x
var pos:CGPoint = self.layer.presentationLayer().bounds.origin
}

func StopSlidingByBeginDragging()
{
CATransaction.begin()
self.myScrollView.layer.removeAllAnimations()
CATransaction.commit()
}

基本上,当我手动触摸或滚动 UIScrollView 时,我需要暂停它应该暂停然后恢复。我发现这个动画是滑动动画,也许你可以建议一些不同的东西。

谢谢

最佳答案

试试这个自定义 UIScrollView 类以及 UIViewController 代码。

class TouchScrollView : UIScrollView
{
var animatedScrollOffset = CGPointMake(0, 0)

func startAnimation()
{
UIScrollView.animateWithDuration(Double(5), delay: 1,
options: (UIViewAnimationOptions.AllowUserInteraction
| UIViewAnimationOptions.CurveLinear |
UIViewAnimationOptions.BeginFromCurrentState),
animations: {
self.contentOffset = self.animatedScrollOffset
},
completion: {
finished in
if !finished {

}
})

}

func stopAnimation()
{
let offset = self.layer.presentationLayer().bounds.origin
self.layer.removeAllAnimations()
self.contentOffset = offset
}

override init(frame: CGRect) {
super.init(frame: frame)

if let gestures = self.gestureRecognizers
{
for gestureRecognizer in gestures
{
if let swipeRecognizer = gestureRecognizer as? UIGestureRecognizer
{
swipeRecognizer.cancelsTouchesInView = false
}
}
}
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.stopAnimation()
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
self.startAnimation()
}
}


class ViewController: UIViewController, UITextViewDelegate,UIScrollViewDelegate {

@IBOutlet var scrollView : TouchScrollView!

override func viewDidLoad() {

scrollView.contentSize = CGSizeMake(scrollView.frame.width, 10000)
scrollView.backgroundColor = UIColor.redColor()
scrollView.showsVerticalScrollIndicator = true
scrollView.showsHorizontalScrollIndicator = true
scrollView.animatedScrollOffset = CGPointMake(0, 1000)
scrollView.startAnimation()
scrollView.delegate = self
}

func scrollViewWillBeginDragging(_: UIScrollView) {
println("begin dragging")
scrollView.stopAnimation()
}

func scrollViewDidEndDragging(_ : UIScrollView, willDecelerate decelerate: Bool) {
println("end dragging")
if !decelerate
{
scrollView.startAnimation()
}
}

func scrollViewDidEndDecelerating(_ : UIScrollView) {
scrollView.startAnimation()
}

}

关于ios - UIView.animateWithDuration 或 UIScrollView.animateWithDuration - 如何在当前状态暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28086267/

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