-1

I am using this code to scroll the scrollview horizontally when sliding the scrollview.

func getScrollView() {
        upperScroll.delegate = self
        upperScroll.isPagingEnabled = true
        //        pageControll.numberOfPages = logoImage.count
        upperScroll.isScrollEnabled = true

        let scrollWidth: Int = Int(self.view.frame.width)

        upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
        print(upperScroll.frame.size.width)
        upperScroll.backgroundColor = UIColor.clear
        for index in 0..<logoImage.count {
            let xPosition = self.view.frame.width * CGFloat(index)
            upperScroll.layoutIfNeeded()
            let img = UIImageView(frame: CGRect(x: CGFloat(xPosition), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
            img.layoutIfNeeded()
            let str = logoImage[index]
            let url = URL(string: str)

            img.sd_setImage(with: url, placeholderImage: UIImage(named:"vbo_logo2.png"))
            upperScroll.addSubview(img)
        }
        view.addSubview(upperScroll)
        upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth) * logoImage.count), height: self.upperScroll.frame.height)
    }

But i want to auto scroll? How can i do this. Can anyone help me please.

user9483522
  • 304
  • 5
  • 20

4 Answers4

6

I got the solution

@objc func animateScrollView() {
        let scrollWidth = upperScroll.bounds.width
        let currentXOffset = upperScroll.contentOffset.x

        let lastXPos = currentXOffset + scrollWidth
        if lastXPos != upperScroll.contentSize.width {
            print("Scroll")
            upperScroll.setContentOffset(CGPoint(x: lastXPos, y: 0), animated: true)
        }
        else {
            print("Scroll to start")
            upperScroll.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
        }
    }

func scheduledTimerWithTimeInterval(){
        // Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
        timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.animateScrollView), userInfo: nil, repeats: true)
    }

and call this function in ViewDidAppear

user9483522
  • 304
  • 5
  • 20
  • 1
    Good job. I added advanced(by: ) after .width and played around with the timeInterval and now I have it sliding nicely – Superlative Dec 01 '19 at 11:07
0

https://developer.apple.com/documentation/uikit/uiscrollview/1619400-setcontentoffset?language=objc

func scrollToPoint(point: CGPoint) {
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(10.0)) {
        upperScroll.setContentOffset(point/* where you want to scroll to*/, animated:true)
    }
}

you can specify the x axis when you are creating the point to pass to the func:
CGPoint(x: 100, y: 0)

Aatish Molasi
  • 2,138
  • 3
  • 20
  • 43
0

There are method available to scroll for visible rect in scrollView. You need to pass CGRect in method, this means area where you want to scroll.

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56
0

How about that:

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

@objc func timerAction() {
        var xOffSet = 0
        xOffSet += 10
        UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
            self.scrollView.contentOffset.x = xOffSet
        }, completion: nil)
    }

How to implement auto scroll in UIScrollview using Swift 3?

Kowboj
  • 351
  • 2
  • 14
  • where we will call timer.fire() @Kowboj – user9483522 Apr 23 '18 at 12:20
  • viewDidLoad? wherever you want it to start – Kowboj Apr 23 '18 at 12:26
  • [_SwiftValue timerAction]: unrecognized selector sent to instance 0x60000065fce0' i am getting this error when i have written this in viewdidload – user9483522 Apr 23 '18 at 13:51
  • Don't use .fire(), sorry. Just put it in viewDidLoad, and add "ViewController." there: let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: (#selector(ViewController.timerAction)), userInfo: nil, repeats: true) – Kowboj Apr 23 '18 at 14:33