-1

I want to implement auto scroll to UIScrollView in swift 3. Could any one help me.

Actually I am new in iOS development.

My Code :

var bannerscView: UIScrollView!
 bannerscView = UIScrollView(frame: CGRect(x: 0, y: 50, width: Int(view.bounds.width), height: 350))
    bannerscView.showsVerticalScrollIndicator = true
    view.addSubview(bannerscView)
    bannerscView.backgroundColor = UIColor.red
    bannerscView.translatesAutoresizingMaskIntoConstraints = false

    _ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(bannerImgScroll), userInfo: nil, repeats: true)

func bannerImgScroll(){


    var xBannerOffset:CGFloat = 5
    for k in 0 ... 5 {

        let bannerImg = UIButton()
        bannerImg.tag = k
        bannerImg.backgroundColor = UIColor.blue //UIColor.darkGray
        bannerImg.setTitle("\(k)", for: .normal)
        //button.addTarget(self, action: #selector(btnTouch), for: UIControlEvents.touchUpInside)

        bannerImg.frame = CGRect(x: xBannerOffset, y: CGFloat(buttonPadding), width: 400, height: 300)
        xBannerOffset = xBannerOffset + CGFloat(buttonPadding) + bannerImg.frame.size.width
        // print("xOffset After : \(xOffset)")
        bannerscView.addSubview(bannerImg)


    }

    bannerscView.contentSize = CGSize(width: xBannerOffset, height: bannerscView.frame.height)
}

Thanks in Advance

5
  • 1
    add more details.
    – Maddy
    Jun 16, 2017 at 9:28
  • see answer , u can use timer for auto scrolling
    – Dhiru
    Jun 16, 2017 at 9:41
  • bannerscView.addSubview(bannerImg) why you are adding SubView every time interval?
    – Dhiru
    Jun 16, 2017 at 9:53
  • Actually I am displaying multiple scrollviews here Jun 16, 2017 at 10:26
  • you can accept the answer by clicking on tick , this will help other who is facing the same problem @RamaChandraMahapatra
    – Dhiru
    Jul 10, 2017 at 6:36

4 Answers 4

4

Start Timer in when you want to start autoScrolling

timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)

Scroll the scrollView after every two second

 func timerAction() {
        yOffSet += 10;
         dispatch_async(dispatch_get_main_queue()) {
  UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
    self.scrollView.contentOffset.y = yOffSet
    }, completion: nil)
}
    }
}
2
  • 1
    Thank you so much, Dhiru for the valuable reply. Its working Jun 16, 2017 at 10:18
  • Its only scroll one time. Apr 16, 2019 at 5:48
1
scroll.setContentOffset(CGPoint(x: xPos, y: yPos), animated: true)
1
  • 1
    Thank you Bali, for your quick reply :) Jun 16, 2017 at 10:20
0

Auto scroll in ScrollView ? Hmm...

Without filling contentSize of ScrollView simply not Possible.Either your doing in View using autolayout or Programmatically.

But you can update the content size of a UIScrollView based on its contained subviews like below

var contentRect = CGRectZero
for view in self.scrollView.subviews {
    contentRect = CGRectUnion(contentRect, view.frame)
}
self.scrollView.contentSize = contentRect.size
1
  • 1
    Thank you DS Dharma for your quick reply :) Jun 16, 2017 at 10:18
0

Swift 4.x

var yOffset: CGFloat = 0

@objc func timerAction() {

    yOffset += 10

    DispatchQueue.main.async {
        UIView.animate(withDuration: 1.0) {
            self.scrollView.contentOffset.y = self.yOffset
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.