Image Slider with UIScrollView

2019-08-16 01:11发布

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
}

Thank you.

2条回答
霸刀☆藐视天下
2楼-- · 2019-08-16 01:31

Please try this code.

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
}

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

查看更多
smile是对你的礼貌
3楼-- · 2019-08-16 01:37
@IBOutlet weak var imageView:UIImageView!

var i=Int()

  override func viewDidLoad() {
        super.viewDidLoad()
        Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(imageChange), userInfo: nil, repeats: true)
        // Do any additional setup after loading the view.
    }

    @objc func imageChange(){
        self.imageView.image=images[i]
        if i<images.count-1{
            i+=1
        }
        else{
            i=0
        }
    }
查看更多
登录 后发表回答