2

I have seen answers for this following question. I am new to swift programming and I am trying to implement a similar feature. I just wondered if you had any guidance on how to achieve this in swift 3 Xcode 8. I have been searching around for a suitable solution but I've had no luck.

I am trying use UIViews as a subview of UIscrollviews. I would also like to have each view fill the screen when pressed and shows another UIView. I have seen a similar feature on the GOLF app by 'Tyler the Creator'

The feature I am trying achieve is pictured below.

Any help you could give would be greatly appreciated.

This is a representation of the feature I am trying to create.

image

Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
user8263946
  • 31
  • 1
  • 1
  • 4
  • 2
    Much much easier if you use `UIPageViewController` than a scroll view. Just about everything is done for you. Easy to find lots of tutorials and samples - just search for `uipageviewcontroller tutorial swift 3` – DonMag Jul 13 '17 at 18:07
  • 1
    Possible duplicate of: https://stackoverflow.com/questions/5618780/uiscrollview-with-pagination-showing-part-of-the-previous-following-pages Tutorial: https://medium.com/@tingyishih/paginated-scroll-view-with-partial-previous-next-page-visible-655159fcdd50 – iOS Blacksmith May 28 '19 at 08:14
  • @user8263946 Hi, Did you find the solution? – Sina Aug 08 '19 at 12:52

3 Answers3

9
    let scrollView : UIScrollView = UIScrollView(frame: CGRect(x: 80, y: 80, 
    width: 250, height: 300))
    scrollView.isPagingEnabled = true
    scrollView.backgroundColor = .orange
    view.addSubview(scrollView)
    let numberOfPages :Int = 5
    let padding : CGFloat = 15
    let viewWidth = scrollView.frame.size.width - 2 * padding
    let viewHeight = scrollView.frame.size.height - 2 * padding

    var x : CGFloat = 0

    for i in 0...numberOfPages{
        let view: UIView = UIView(frame: CGRect(x: x + padding, y: padding, width: viewWidth, height: viewHeight))
        view.backgroundColor = UIColor.white
        scrollView .addSubview(view)

        x = view.frame.origin.x + viewWidth + padding
    }

    scrollView.contentSize = CGSize(width:x+padding, height:scrollView.frame.size.height)
user3202263
  • 167
  • 1
  • 7
0
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

var colors:[UIColor] = [.red, .blue, .green, .yellow]
var frame: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.isPagingEnabled = true
    scrollView.backgroundColor = .orange
    view.addSubview(scrollView)
    let numberOfPages :Int = 3
    let padding : CGFloat = 15
    let viewWidth = scrollView.frame.size.width - 2 * padding
    let viewHeight = scrollView.frame.size.height - 2 * padding

    var x : CGFloat = 0

    for i in 0...numberOfPages{
        let view: UIView = UIView(frame: CGRect(x: x + padding, y: padding, width: viewWidth, height: viewHeight))
        view.backgroundColor = colors[i]
        scrollView .addSubview(view)
        x = view.frame.origin.x + viewWidth + padding
    }

    scrollView.contentSize = CGSize(width:x+padding, height:scrollView.frame.size.height)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}
Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45
Aditya
  • 17
  • 1
0

You should use UIPageViewController. That class is exactly meant to do what you are looking for with ease. See UIPageViewController

You can embed your UIPageViewController in a UIContainerView to fit it anywhere.

zouritre
  • 231
  • 3
  • 6