UIView.animateWithDuration 或 UIScrollView.animateWithDuration - 如何在当前状态暂停
Posted
技术标签:
【中文标题】UIView.animateWithDuration 或 UIScrollView.animateWithDuration - 如何在当前状态暂停【英文标题】:UIView.animateWithDuration or UIScrollView.animateWithDuration - how to pause at current status 【发布时间】:2015-01-22 10:18:55 【问题描述】:我正在使用这些(代码块 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 将到达 endPoint(到 x:1000)
另外animateWithDuration是取消scrollViewDidScroll,所以无法获取当前位置。另外,同样的原因,在暂停功能中无法获取当前位置。
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 时,我需要暂停它应该暂停然后恢复。我发现这个动画是滑动动画,也许你可以提出一些不同的建议。
谢谢
【问题讨论】:
【参考方案1】:试试这个自定义的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()
【讨论】:
以上是关于UIView.animateWithDuration 或 UIScrollView.animateWithDuration - 如何在当前状态暂停的主要内容,如果未能解决你的问题,请参考以下文章