带有 UIScrollView 的图像滑块 [英] Image Slider with UIScrollView

查看:23
本文介绍了带有 UIScrollView 的图像滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个每 2 秒自动滑动一次的图像滑块.我写的代码立即移动到下一张没有幻灯片动画的图像,我想通过显示滚动视图滚动来以某种方式修复它,这是我的代码:

I am making an image slider that slides automatically every 2 seconds. The code I have wrote moves immediately to the next image without the slide animation, I want to fix it somehow by showing the scrollview scrolling, this is my code:

var imagesArray = [UIImage]()
static var i = 0

override func viewDidLoad() {
    super.viewDidLoad()
    imagesArray = [image1, image2, image3, image4]
    for i in 0..<imagesArray.count{
        let imageview = UIImageView()
        imageview.image = imagesArray[i]
        imageview.contentMode = .scaleAspectFit
        let xPosition = self.view.frame.width * CGFloat(i)
        imageview.frame = CGRect(x: xPosition, y: 0, width: self.imagesScrollView.frame.width, height: self.imagesScrollView.frame.height)
        imagesScrollView.contentSize.width = imagesScrollView.frame.width * CGFloat(i+1)
        imagesScrollView.addSubview(imageview)
    }
}

override func viewDidAppear(_ animated: Bool) {
    let scrollingTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(MenuViewController.newStartScrolling), userInfo: nil, repeats: true)
    scrollingTimer.fire()

func newStartScrolling()
{
    if MenuViewController.i == imagesArray.count {
        MenuViewController.i = 0
    }
    let x = CGFloat(MenuViewController.i) * imagesScrollView.frame.size.width
    imagesScrollView.contentOffset = CGPoint(x: x, y: 0)
    MenuViewController.i += 1
}

谢谢.

推荐答案

请试试这个代码.

override func viewDidAppear(_ animated: Bool) {

    imagesArray = [UIImage(named: "apple.jpg")!, UIImage(named: "empire.jpg")!, UIImage(named: "ottawa.jpg")!]

    imagesScrollView.contentSize = CGSize(width: imagesScrollView.frame.width * CGFloat(imagesArray.count), height: imagesScrollView.frame.height)
    for i in 0..<imagesArray.count{
        let imageview = UIImageView()
        imageview.image = imagesArray[i]
        imageview.contentMode = .scaleAspectFill
        imageview.clipsToBounds = true
        let xPosition = self.imagesScrollView.frame.width * CGFloat(i)
        imageview.frame = CGRect(x: xPosition, y: 0, width: self.imagesScrollView.frame.width, height: self.imagesScrollView.frame.height)
        print(imageview)
        imagesScrollView.addSubview(imageview)
    }

    let scrollingTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.newStartScrolling), userInfo: nil, repeats: true)
    scrollingTimer.fire()
}
func newStartScrolling()
{
    if ViewController.i == imagesArray.count {
        ViewController.i = 0
    }
    let x = CGFloat(ViewController.i) * imagesScrollView.frame.size.width
    imagesScrollView.setContentOffset(CGPoint(x: x, y: 0), animated: true)
    ViewController.i += 1
}

你必须使用 func setContentOffset(_ contentOffset: CGPoint, animation: Bool) 方法来使用动画改变内容偏移.

you have to use func setContentOffset(_ contentOffset: CGPoint, animated: Bool) method to change content offset using animation.

这篇关于带有 UIScrollView 的图像滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆