ScrollView对齐中心

时间:2018-06-26 14:55:54

标签: ios swift uiscrollview

我想像这样的视频一样设置我的scrollView(出于某种原因,我知道在视频中使用collectionview bu

https://www.dropbox.com/s/kj6xgj68gz6258p/ScreenRecording_06-26-2018%2019-15-43.MP4

三件重要的事情:

  1. 滚动时滚动必须对齐中心
  2. 调整当前项目的大小(前进项目大于后退项目)
  3. 迅速

更新:

var ScrollView = UIScrollView()

var arr : Array<UIColor> = [.red,.blue,.green]

override func viewDidLoad() {
    super.viewDidLoad()

    ScrollView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(ScrollView)

    ScrollView.widthAnchor.constraint(equalToConstant: self.view.frame.size.width).isActive = true
    ScrollView.heightAnchor.constraint(equalToConstant: 320).isActive = true

    ScrollView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive = true

    ScrollView.alwaysBounceVertical = false

    ScrollView.isPagingEnabled = true
    ScrollView.contentSize = CGSize(width: 3.0 * self.view.frame.size.width, height: 320)
    ScrollView.contentOffset = CGPoint(x: view.frame.size.width, y: 0)


    ScrollView.center = self.view.center

    for i in 0..<3 {
        let viewBG = UIView()
        viewBG.backgroundColor = arr[i]

        if (i == 0) {
            viewBG.frame = CGRect(x: view.frame.size.width * CGFloat(i) + 125, y: 0, width: view.frame.size.width - 100, height: 320)
        } else if(i == 1) {

            viewBG.frame = CGRect(x: view.frame.size.width * CGFloat(i) + 50, y: 0, width: view.frame.size.width - 100, height: 320)
        } else {

            viewBG.frame = CGRect(x: view.frame.size.width * CGFloat(i) - 25, y: 0, width: view.frame.size.width - 100, height: 320)
        }



        ScrollView.addSubview(viewBG)
    }
}

谢谢大家

1 个答案:

答案 0 :(得分:0)

使用iCarousal。易于实现,并提供了Swift示例。

以下是链接- https://github.com/nicklockwood/iCarousel