如何水平自动滚动scrollView?

时间:2018-04-23 06:27:45

标签: ios swift autoscroll

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

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

但我想自动滚动? 我怎样才能做到这一点。任何人都可以帮助我。

4 个答案:

答案 0 :(得分:1)

我得到了解决方案

@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中调用此函数

答案 1 :(得分: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)
    }
}

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

怎么样:

protected void Page_Load()

    {
      Button ButtonChange = new Button();

      ButtonChange.Text = "New Button";
      ButtonChange.ID = "btnNew_" + i.ToString();
      ButtonChange.Font.Size = FontUnit.Point(7);
      ButtonChange.ControlStyle.CssClass = "button";
      ButtonChange.Click += new EventHandler(test);
    }

http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx