Advertisement
Don_Mag

Untitled

Sep 28th, 2023
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.24 KB | None | 0 0
  1. class MoveImageScrollVC: UIViewController, UIScrollViewDelegate {
  2.    
  3.     var exampleConstraint: NSLayoutConstraint!
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         let scrollView = UIScrollView()
  9.         scrollView.backgroundColor = .systemYellow
  10.         scrollView.translatesAutoresizingMaskIntoConstraints = false
  11.         view.addSubview(scrollView)
  12.        
  13.         let g = view.safeAreaLayoutGuide
  14.         let cg = scrollView.contentLayoutGuide
  15.         let fg = scrollView.frameLayoutGuide
  16.        
  17.         NSLayoutConstraint.activate([
  18.             // constrain scroll view to safe area, inset by 20-points
  19.             scrollView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  20.             scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  21.             scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  22.             scrollView.widthAnchor.constraint(equalToConstant: 100.0),
  23.         ])
  24.        
  25.         let tallLabel = UILabel()
  26.         tallLabel.backgroundColor = .cyan
  27.         tallLabel.numberOfLines = 0
  28.         tallLabel.textAlignment = .center
  29.         tallLabel.font = .systemFont(ofSize: 24.0, weight: .light)
  30.        
  31.         var str: String = "1"
  32.         for i in 2...50 {
  33.             str += "\n\(i)"
  34.         }
  35.         tallLabel.text = str
  36.        
  37.         tallLabel.translatesAutoresizingMaskIntoConstraints = false
  38.         scrollView.addSubview(tallLabel)
  39.        
  40.         NSLayoutConstraint.activate([
  41.             tallLabel.topAnchor.constraint(equalTo: cg.topAnchor, constant: 8.0),
  42.             tallLabel.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 8.0),
  43.             tallLabel.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: 0.0),
  44.             tallLabel.bottomAnchor.constraint(equalTo: cg.bottomAnchor, constant: -8.0),
  45.             tallLabel.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: -16.0),
  46.         ])
  47.        
  48.         let anchorView = UILabel()
  49.         anchorView.backgroundColor = .yellow
  50.         anchorView.textAlignment = .center
  51.         anchorView.text = "Anchor View"
  52.  
  53.         let constrainedView = UIImageView()
  54.         if let img = UIImage(named: "sampleImage") {
  55.             constrainedView.image = img
  56.         }
  57.        
  58.         anchorView.translatesAutoresizingMaskIntoConstraints = false
  59.         view.addSubview(anchorView)
  60.         constrainedView.translatesAutoresizingMaskIntoConstraints = false
  61.         view.addSubview(constrainedView)
  62.        
  63.         exampleConstraint = constrainedView.bottomAnchor.constraint(equalTo: anchorView.topAnchor, constant: 0.0)
  64.         exampleConstraint.priority = .required - 1
  65.        
  66.         NSLayoutConstraint.activate([
  67.            
  68.             anchorView.leadingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
  69.             anchorView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  70.             anchorView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0),
  71.             anchorView.heightAnchor.constraint(equalToConstant: 40.0),
  72.  
  73.             exampleConstraint,
  74.             constrainedView.topAnchor.constraint(greaterThanOrEqualTo: scrollView.topAnchor),
  75.             constrainedView.leadingAnchor.constraint(equalTo: anchorView.leadingAnchor, constant: 0.0),
  76.             constrainedView.trailingAnchor.constraint(equalTo: anchorView.trailingAnchor, constant: 0.0),
  77.             constrainedView.heightAnchor.constraint(equalToConstant: 120.0),
  78.         ])
  79.        
  80.         scrollView.delegate = self
  81.     }
  82.    
  83.     func scrollViewDidScroll(_ scrollView: UIScrollView) {
  84.         let up = scrollView.contentOffset.y
  85.         print("up \(up)")
  86.         exampleConstraint.constant = -up
  87.     }
  88.    
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement