以随机延迟(一个接一个)和随机水平位置动画滚动图像
Animate scrolling images with random delay (one after another) and random horizontal position
我有一张包含 x 张图片的列表。对于每个图像,我创建一个 UIImageView,其中 y 是视图高度,随机 x
func computeImageFrame() -> CGRect {
let imageWidth: CGFloat = 91
let imageHeight: CGFloat = 131
var x = xPos()
let y = yPos()
if (x > ((self.backgroundView?.bounds.width)! - (self.backgroundView?.bounds.width)! * 0.20)){
x = x.truncatingRemainder(dividingBy: ((self.backgroundView?.bounds.width)! - (self.backgroundView?.bounds.width)! * 0.20))}
return CGRect(x: x, y: y, width: imageWidth, height: imageHeight)
}
哪里
func xPos() -> CGFloat {
return CGFloat(arc4random_uniform(UInt32((self.backgroundView?.frame.size.width)!)))
}
func yPos() -> CGFloat {
return self.backgroundView.bounds.height
}
此时,我在 UIImageViews 上有一个列表,其中 x 是随机的,y 是 viewHeight,现在我正在尝试制作动画
func animateBackgroundFor(_ imageViews: [UIImageView]) {
for imageView in imageViews {
self.backgroundView.addSubview(imageView)
}
// Animate background
UIView.animate(withDuration: 6.0, delay: 0.0, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.curveLinear], animations: {
for imageView in imageViews {
imageView.frame = imageView.frame.offsetBy(dx: 0.0, dy: -7 * imageView.frame.size.height)
}
}, completion: nil)
}
我的问题是如何让它们一个接一个地拍摄,而不是同时拍摄,以及如何在每次循环时重新计算每张图像的水平位置。
谢谢
编辑:忘记说了,我试过了
func animateBackgroundFor(_ imageViews: [UIImageView]) {
for imageView in imageViews {
self.backgroundView.addSubview(imageView)
}
for imageView in imageViews {
UIView.animate(withDuration: 6.0, delay: 1.2, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.curveLinear], animations: {
imageView.frame = imageView.frame.offsetBy(dx: 0.0, dy: -7 * imageView.frame.size.height)
}, completion: nil)
}
}
尝试以下解决方案。
添加到您的控制器私有 属性:
private var imageViews = [UIImageView]()
然后这样修改你的animateBackgroundFor(_)
方法:
func animateBackgroundFor(_ imageViews: [UIImageView]) {
for imageView in imageViews {
self.backgroundView.addSubview(imageView)
}
self.imageViews = imageViews
animateImage(at: 0)
}
func animateImage(at index: NSInteger) {
guard index >= 0 && index < imageViews.count else { return }
let imageView = imageViews[index]
imageView.frame = computeImageFrame()
UIView.animate(withDuration: 6.0,
delay: 0.0,
options: UIViewAnimationOptions.curveLinear,
animations: {
imageView.frame = imageView.frame.offsetBy(dx: 0.0, dy: -7 * imageView.frame.size.height)
},
completion: {
[weak self] finished in
guard finished && self != nil else { return }
let nextIndex = index + 1 < self!.imageViews.count ? index + 1 : 0
self!.animateImage(at: nextIndex)
})
}
我有一张包含 x 张图片的列表。对于每个图像,我创建一个 UIImageView,其中 y 是视图高度,随机 x
func computeImageFrame() -> CGRect {
let imageWidth: CGFloat = 91
let imageHeight: CGFloat = 131
var x = xPos()
let y = yPos()
if (x > ((self.backgroundView?.bounds.width)! - (self.backgroundView?.bounds.width)! * 0.20)){
x = x.truncatingRemainder(dividingBy: ((self.backgroundView?.bounds.width)! - (self.backgroundView?.bounds.width)! * 0.20))}
return CGRect(x: x, y: y, width: imageWidth, height: imageHeight)
}
哪里
func xPos() -> CGFloat {
return CGFloat(arc4random_uniform(UInt32((self.backgroundView?.frame.size.width)!)))
}
func yPos() -> CGFloat {
return self.backgroundView.bounds.height
}
此时,我在 UIImageViews 上有一个列表,其中 x 是随机的,y 是 viewHeight,现在我正在尝试制作动画
func animateBackgroundFor(_ imageViews: [UIImageView]) {
for imageView in imageViews {
self.backgroundView.addSubview(imageView)
}
// Animate background
UIView.animate(withDuration: 6.0, delay: 0.0, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.curveLinear], animations: {
for imageView in imageViews {
imageView.frame = imageView.frame.offsetBy(dx: 0.0, dy: -7 * imageView.frame.size.height)
}
}, completion: nil)
}
我的问题是如何让它们一个接一个地拍摄,而不是同时拍摄,以及如何在每次循环时重新计算每张图像的水平位置。
谢谢
编辑:忘记说了,我试过了
func animateBackgroundFor(_ imageViews: [UIImageView]) {
for imageView in imageViews {
self.backgroundView.addSubview(imageView)
}
for imageView in imageViews {
UIView.animate(withDuration: 6.0, delay: 1.2, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.curveLinear], animations: {
imageView.frame = imageView.frame.offsetBy(dx: 0.0, dy: -7 * imageView.frame.size.height)
}, completion: nil)
}
}
尝试以下解决方案。 添加到您的控制器私有 属性:
private var imageViews = [UIImageView]()
然后这样修改你的animateBackgroundFor(_)
方法:
func animateBackgroundFor(_ imageViews: [UIImageView]) {
for imageView in imageViews {
self.backgroundView.addSubview(imageView)
}
self.imageViews = imageViews
animateImage(at: 0)
}
func animateImage(at index: NSInteger) {
guard index >= 0 && index < imageViews.count else { return }
let imageView = imageViews[index]
imageView.frame = computeImageFrame()
UIView.animate(withDuration: 6.0,
delay: 0.0,
options: UIViewAnimationOptions.curveLinear,
animations: {
imageView.frame = imageView.frame.offsetBy(dx: 0.0, dy: -7 * imageView.frame.size.height)
},
completion: {
[weak self] finished in
guard finished && self != nil else { return }
let nextIndex = index + 1 < self!.imageViews.count ? index + 1 : 0
self!.animateImage(at: nextIndex)
})
}