UIView.animateWithDuration 或 UIScrollView.animateWithDuration - 如何在当前状态暂停
UIView.animateWithDuration or UIScrollView.animateWithDuration - how to pause at current status
我正在使用这些(代码块 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
class 以及 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()
}
}
我正在使用这些(代码块 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
class 以及 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()
}
}