2017-05-27 13 views
1

私はx画像のリストを持っています。 Iは、YとUIImageViewを作成し、各像視野高さとランダムXこの時点でランダムな遅延(1つずつ)とランダムな水平位置のアニメーションスクロール画像

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 
} 

ため、I xはランダムであり、yはviewHeightでありUIImageViewsのリストを持っています今私は私の質問は、私は彼らが次々と撃つのではなく、同時にするために得ることができる方法で、どのように私が今までで、各画像の水平位置を再計算することができます

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) 
} 

をアニメーション化しようとしていますyループ。

はあなたに

編集ありがとう:言及するのを忘れ、私は次の解決方法を試してみてください

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) 
    } 
} 

答えて

2

を試してみました。 は、コントローラのプライベートプロパティに追加します。その後、

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) 
    }) 
} 
素晴らしい作品
+0

!ありがとう! –

関連する問題