Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5. @IBOutlet weak var mainScrollview: UIScrollView!
  6.  
  7. var imageArray = [UIImage]()
  8.  
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11.  
  12. imageArray = [#imageLiteral(resourceName: "3"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "1")]
  13. //Store the images which you want to display in an array
  14.  
  15. for i in 0..<imageArray.count{
  16.  
  17. let imageView = UIImageView() //set a constant "imageView"
  18. imageView.image = imageArray[i]
  19. imageView.contentMode = .scaleAspectFit //The images will not looks so strange with AspectFit
  20. 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
  21. imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollview.frame.width, height: self.mainScrollview.frame.height)
  22. //x value is special here, bcoz we use it as scrolling. That's why we go back and set the constant xPosition
  23.  
  24. mainScrollview.contentSize.width = mainScrollview.frame.width * CGFloat(i+1) //Don't want i multiply by 0
  25. mainScrollview.addSubview(imageView)
  26. }
  27. }
  28.  
  29. override func didReceiveMemoryWarning() {
  30. super.didReceiveMemoryWarning()
  31. // Dispose of any resources that can be recreated.
  32. }
  33.  
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement