背景图像动画滚动

时间:2016-11-23 16:03:19

标签: ios swift animation

我有一张图像(5-6张图像),应该以不同的速度在背景上进行动画滚动。我把它们放在UIView上而不是UIScrollView中。

现在我有这个方法: Method screen

我在用户看到的屏幕上添加图像并在屏幕后面复制图像。但是因为图像和copyImages(屏幕后面)必须通过相同的距离它们有不同的速度(不是我放的)。 所以它对我不起作用。

有人可以提供帮助吗?

P.S。有些图像有不同的大小,所以我需要为它们更改yPos。仅供详细说明。

func setUpBackGroundLayersWithArray(){

    var xPos: CGFloat = 0

    for  (index,image) in self.backGroundsImages.reverse().enumerate(){
        var yPos:CGFloat = -30

        switch index {
        case 1: yPos = -10
                xPos = 320
        case 2: yPos = -10
        default: yPos = -30
        }

        let imageView = UIImageView(image: image)
        imageView.frame = CGRectMake(xPos, yPos, image.size.width, image.size.height)
        imageView.contentMode = .ScaleAspectFit
        self.addSubview(imageView)

        let copyimageView = UIImageView(image: image)
        copyimageView.frame = CGRectMake(320, yPos, image.size.width, image.size.height)
        copyimageView.contentMode = .ScaleAspectFit
        self.addSubview(copyimageView)

        self.layoutIfNeeded()
        let animator = UIViewPropertyAnimator(duration:8 - Double(index + 1), curve: .Linear, animations: {
            UIView.animateKeyframesWithDuration(0, delay: 0, options: [.Repeat], animations: {
                imageView.frame = CGRectMake(0 - copyimageView.frame.size.width, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)
                }, completion: nil)
        })
        let secondAnimator = UIViewPropertyAnimator(duration:10 - Double(index + 1), curve: .Linear, animations: {
            UIView.animateKeyframesWithDuration(0, delay: 0, options: [.Repeat], animations: {
                copyimageView.frame = CGRectMake(0-copyimageView.frame.size.width,  copyimageView.frame.origin.y,  copyimageView.frame.size.width,  copyimageView.frame.size.height)
                }, completion: nil)
        })

        self.animators.append(animator)
        self.animators.append(secondAnimator)
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用UICollectionView对图片进行简单的显示和动画制作。对于动画,您需要在需要的时间间隔后使用scrollToItemAtIndexPath:atScrollPosition:animated:方法

相关问题