Skip to content

Instantly share code, notes, and snippets.

@filimo
Last active January 31, 2019 13:06
Show Gist options
  • Save filimo/b6b12022646d77ad94b4572308c9f7c1 to your computer and use it in GitHub Desktop.
Save filimo/b6b12022646d77ad94b4572308c9f7c1 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// UIScrollView
//
// Created by VKushnerov on 1/31/19.
// Copyright © 2019 VKushnerov. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var myScrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
let scrollViewRect = view.bounds
myScrollView = UIScrollView(frame: scrollViewRect)
myScrollView.isPagingEnabled = true
myScrollView.contentSize = CGSize(width: scrollViewRect.size.width * 3, height: scrollViewRect.size.height)
view.addSubview(myScrollView)
let images = ["iphone", "ipad", "macbook"]
for imageName in images {
addNewImageView(imageName: imageName)
}
}
func addNewImageView(imageName: String) {
var imageViewRect = view.bounds
let image = UIImage(named: imageName)
imageViewRect.origin.x = imageViewRect.size.width * CGFloat(myScrollView.subviews.count)
let imageView = newImageViewWithImage(paramImage: image!, paramFrame: imageViewRect)
myScrollView.addSubview(imageView)
}
func newImageViewWithImage(paramImage: UIImage, paramFrame: CGRect) -> UIImageView {
let result = UIImageView(frame: paramFrame)
result.contentMode = .scaleAspectFit
result.image = paramImage
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment