Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var mainScrollview: UIScrollView!
- var imageArray = [UIImage]()
- override func viewDidLoad() {
- super.viewDidLoad()
- imageArray = [#imageLiteral(resourceName: "3"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "1")]
- //Store the images which you want to display in an array
- for i in 0..<imageArray.count{
- let imageView = UIImageView() //set a constant "imageView"
- imageView.image = imageArray[i]
- imageView.contentMode = .scaleAspectFit //The images will not looks so strange with AspectFit
- let xPosition = self.view.frame.width * CGFloat(i) //i is an integer, so we change it into a CGFloat, or it will rise an error
- imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollview.frame.width, height: self.mainScrollview.frame.height)
- //x value is special here, bcoz we use it as scrolling. That's why we go back and set the constant xPosition
- mainScrollview.contentSize.width = mainScrollview.frame.width * CGFloat(i+1) //Don't want i multiply by 0
- mainScrollview.addSubview(imageView)
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement