不同大小的scrollview页面

时间:2017-06-26 12:08:15

标签: ios swift uiscrollview

我喜欢从UIScrollView设置一个比其他页面更大的页面。最后一页应该与整个UIView的大小相同,可能吗?

这是我未完成的代码:

    self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
    let scrollviewHeight = self.scrollView.frame.height
    let scrollviewWidth = self.scrollView.frame.width

    let viewHeight = self.view.frame.height
    let viewWidth = self.view.frame.width

    let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
    let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
    let imgTree = UIImageView(frame: CGRect(x: scrollviewWidth*2, y: 0, width: scrollviewWidth, height: scrollviewHeight))
    let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: viewWidth, height: viewHeight))

    imgOne.image = UIImage(named: "preview1")
    imgTwo.image = UIImage(named: "preview2")
    imgTree.image = UIImage(named: "preview3")

    self.scrollView.addSubview(imgOne)
    self.scrollView.addSubview(imgTwo)
    self.scrollView.addSubview(imgTree)
    self.scrollView.addSubview(feedbackView)

    self.addChildFeedBack(feedbackView: feedbackView)

    self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 4, height: self.scrollView.frame.height)
    self.scrollView.isPagingEnabled = true

feedbackView就是那个,应该是最大的。

1 个答案:

答案 0 :(得分:1)

我不知道这是否会为您提供您想要的结果类型,但需要尝试...

不是将feedbackView添加到scrollView,而是添加另一个UIScrollView作为第4个子视图,然后添加feedbackView作为的子视图滚动视图:

    // your original setup code
    self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
    let scrollviewHeight = self.scrollView.frame.height
    let scrollviewWidth = self.scrollView.frame.width

    let viewHeight = self.view.frame.height
    let viewWidth = self.view.frame.width

    let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
    let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
    let imgTree = UIImageView(frame: CGRect(x: scrollviewWidth*2, y: 0, width: scrollviewWidth, height: scrollviewHeight))
    let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: viewWidth, height: viewHeight))

    imgOne.image = UIImage(named: "preview1")
    imgTwo.image = UIImage(named: "preview2")
    imgTree.image = UIImage(named: "preview3")

    self.scrollView.addSubview(imgOne)
    self.scrollView.addSubview(imgTwo)
    self.scrollView.addSubview(imgTree)

    // don't add feedbackView to scrollView
    // self.scrollView.addSubview(feedbackView)

    // instead, create another scroll view, 
    //    add feedbackView to that scroll view, 
    //    and add that scroll view as the 4th "page" in scrollView

    let fbScrollView = UIScrollView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: scrollviewWidth, height: scrollviewHeight))

    // just so we can see where it is during development
    fbScrollView.backgroundColor = .purple

    // feedbackView will now be in its own scroll view, so
    // set its origin to 0,0
    feedbackView.frame.origin = CGPoint.zero

    fbScrollView.addSubview(feedbackView)
    fbScrollView.contentSize = feedbackView.frame.size

    // add the new scroll view to the "paging" scrollView
    self.scrollView.addSubview(fbScrollView)

    // and the rest of your setup
    self.addChildFeedBack(feedbackView: feedbackView)

    self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 4, height: self.scrollView.frame.height)
    self.scrollView.isPagingEnabled = true