Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用随机延迟(一个接一个)和随机水平位置设置滚动图像的动画_Swift_Animation_Uiview_Uiviewanimation - Fatal编程技术网

Swift 使用随机延迟(一个接一个)和随机水平位置设置滚动图像的动画

Swift 使用随机延迟(一个接一个)和随机水平位置设置滚动图像的动画,swift,animation,uiview,uiviewanimation,Swift,Animation,Uiview,Uiviewanimation,我有一张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)!

我有一张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)
}
在哪里

现在,我在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 imageview=[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)
    })
} 
func animateBackgroundFor(ImageView:[UIImageView]){
对于imageView中的imageView{
self.backgroundView.addSubview(imageView)
}
self.imageview=imageview
动画图像(位于:0)
}
func animateImage(索引处:NSInteger){
保护索引>=0&&index
效果很好!谢谢
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)
    })
}