为什么重复 UIView 动画会在边缘滑动时冻结?
Why does repeating UIView animation freeze on edge swipe?
我正在尝试创建一个视图,该视图可以连续从右向左滚动图像,从而有效地创建无限滚动的背景。我使用两个相邻的图像视图构建了这个,并使用 UIView
动画设置为 .Repeat
以负水平偏移转换帧。 (下面的代码)
效果很好!直到我尝试在导航控制器中进行边缘滑动,此时动画冻结:
我的代码如下:
class ScrollingImageView: UIView {
required init?(coder aDecoder: NSCoder) { fatalError() }
let imageView = UIImageView()
let imageView2 = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
imageView.image = UIImage(named: "mario")
imageView2.image = UIImage(named: "mario")
imageView.frame = bounds
imageView2.frame = CGRectOffset(bounds, bounds.width, 0)
addSubview(imageView)
addSubview(imageView2)
}
func play() {
UIView.animateWithDuration(5, delay: 0, options: [.Repeat, .CurveLinear], animations: {
self.imageView.frame = CGRectOffset(self.imageView.frame, -self.bounds.width, 0)
self.imageView2.frame = CGRectOffset(self.imageView2.frame, -self.bounds.width, 0)
}, completion: nil)
}
}
为什么当这个交互开始时动画停止了?我认为它 某些东西 与将 CALayer
上的速度设置为零的交互式手势有关。有没有办法让这个动画一直播放?
正如您所说,您使用的是交互式手势。它是如何工作的?它冻结动画(层 speed
为零)并更改其 "frame"(层 timeOffset
)以匹配手势的当前位置。
要解决此问题,您或许可以将动画放入不同的层,但我不确定。我确定 会 工作的方式是自己实现整个自定义过渡交互 — 而不是 使用 UIPercentDrivenInteractiveTransition(冻结动画)但随着过渡的进行,请自行定位视图。
我正在尝试创建一个视图,该视图可以连续从右向左滚动图像,从而有效地创建无限滚动的背景。我使用两个相邻的图像视图构建了这个,并使用 UIView
动画设置为 .Repeat
以负水平偏移转换帧。 (下面的代码)
效果很好!直到我尝试在导航控制器中进行边缘滑动,此时动画冻结:
我的代码如下:
class ScrollingImageView: UIView {
required init?(coder aDecoder: NSCoder) { fatalError() }
let imageView = UIImageView()
let imageView2 = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
imageView.image = UIImage(named: "mario")
imageView2.image = UIImage(named: "mario")
imageView.frame = bounds
imageView2.frame = CGRectOffset(bounds, bounds.width, 0)
addSubview(imageView)
addSubview(imageView2)
}
func play() {
UIView.animateWithDuration(5, delay: 0, options: [.Repeat, .CurveLinear], animations: {
self.imageView.frame = CGRectOffset(self.imageView.frame, -self.bounds.width, 0)
self.imageView2.frame = CGRectOffset(self.imageView2.frame, -self.bounds.width, 0)
}, completion: nil)
}
}
为什么当这个交互开始时动画停止了?我认为它 某些东西 与将 CALayer
上的速度设置为零的交互式手势有关。有没有办法让这个动画一直播放?
正如您所说,您使用的是交互式手势。它是如何工作的?它冻结动画(层 speed
为零)并更改其 "frame"(层 timeOffset
)以匹配手势的当前位置。
要解决此问题,您或许可以将动画放入不同的层,但我不确定。我确定 会 工作的方式是自己实现整个自定义过渡交互 — 而不是 使用 UIPercentDrivenInteractiveTransition(冻结动画)但随着过渡的进行,请自行定位视图。