如何水平自动滚动 scrollView?

How to autoscroll the scrollView horizontally?

我正在使用这段代码在滑动滚动视图时水平滚动滚动视图。

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)
    }

但是我想自动滚动? 我怎样才能做到这一点。谁能帮帮我。

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)
    }
}

您可以在创建要传递给函数的点时指定 x 轴:
CGPoint(x: 100, y: 0)

scrollView 中有滚动可见 rect 的方法。您需要在方法中传递 CGRect,这意味着您要滚动的区域。

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;

怎么样:

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?

我得到了解决方案

@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)
    }

并在 ViewDidAppear 中调用此函数