UIView.animate与UIScrollView内的imageArray一起使用,但具有分页启用的效果

时间:2018-12-18 15:53:38

标签: ios swift animation

我是Swift的新手,但我有一些基本的经验。 我已经成功创建了包含三个图像的动画,并且它们重复了自己。但是我希望这种重复的效果像在UIScrollView的属性检查器中检查分页启用时一样。

在选项中:UIView.AnimationOptions我尝试了其他常数,但找不到适合我的东西。

我的问题:是否可以像启用滑动页面滚动一样在滚动视图中滑动图像那样对图像阵列进行动画处理?

class ViewController: UIViewController {

    @IBOutlet weak var mainScrollView: UIScrollView!

    var imageArray = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        imageArray = [UIImage(named: "forest")!, UIImage(named: "slika1")!, UIImage(named: "slika2")!]

        for i in 0..<imageArray.count {

            let imageView = UIImageView()
            imageView.image = imageArray[i]
            imageView.contentMode = .scaleAspectFit
            let xPosition = self.miniView.frame.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)

            mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
            mainScrollView.addSubview(imageView)

        }

        startAnimating()
    }

    func startAnimating() {
        var newOffset = mainScrollView.contentOffset
        newOffset.x = 0.0
        newOffset.x += mainScrollView.frame.width * CGFloat(imageArray.count - 1)
        UIView.animate(withDuration: Double(imageArray.count), delay: 5, options: [.repeat, .allowUserInteraction], animations: {
            self.mainScrollView.contentOffset = newOffset
        })
    }

}

1 个答案:

答案 0 :(得分:0)

好吧,如果我正确地理解了你想要什么,这是正确的答案。 逐页制作动画,其中一个动画和另一个动画的延迟时间为5秒。

您需要使用Timer.scheduledTimer来触发滚动到下一页的方法。使用该解决方案,您可以将分页启用检查为开,以便用户可以更改页面本身。

我希望对您有帮助

class ViewController: UIViewController {

        @IBOutlet weak var mainScrollView: UIScrollView!

        var imageArray = [UIImage]()

        var currentPage = 0

        override func viewDidLoad() {
            super.viewDidLoad()

            imageArray = [UIImage(named: "forest")!, UIImage(named: "slika1")!, UIImage(named: "slika2")!]

            for i in 0..<imageArray.count {

                let imageView = UIImageView()
                imageView.image = imageArray[i]
                imageView.contentMode = .scaleAspectFit
                let xPosition = self.miniView.frame.width * CGFloat(i)
                imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)

                mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
                mainScrollView.addSubview(imageView)

            }

            Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(startAnimating), userInfo: nil, repeats: true)
        }

        @objc func startAnimating() {

           //that line check if the current page is the last one, and set it to the first, otherwise, it increment the currentPage
           self.currentPage = (self.currentPage == imageArray.count-1) ? 0 : self.currentPage+1


           var newOffset = mainScrollView.contentOffset
           newOffset.x = mainScrollView.frame.width * CGFloat(self.currentPage)

           self.mainScrollView.setContentOffset(newOffset, animated: true)
       }

    }