Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MoveImageScrollVC: UIViewController, UIScrollViewDelegate {
- var exampleConstraint: NSLayoutConstraint!
- override func viewDidLoad() {
- super.viewDidLoad()
- let scrollView = UIScrollView()
- scrollView.backgroundColor = .systemYellow
- scrollView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(scrollView)
- let g = view.safeAreaLayoutGuide
- let cg = scrollView.contentLayoutGuide
- let fg = scrollView.frameLayoutGuide
- NSLayoutConstraint.activate([
- // constrain scroll view to safe area, inset by 20-points
- scrollView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
- scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
- scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
- scrollView.widthAnchor.constraint(equalToConstant: 100.0),
- ])
- let tallLabel = UILabel()
- tallLabel.backgroundColor = .cyan
- tallLabel.numberOfLines = 0
- tallLabel.textAlignment = .center
- tallLabel.font = .systemFont(ofSize: 24.0, weight: .light)
- var str: String = "1"
- for i in 2...50 {
- str += "\n\(i)"
- }
- tallLabel.text = str
- tallLabel.translatesAutoresizingMaskIntoConstraints = false
- scrollView.addSubview(tallLabel)
- NSLayoutConstraint.activate([
- tallLabel.topAnchor.constraint(equalTo: cg.topAnchor, constant: 8.0),
- tallLabel.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 8.0),
- tallLabel.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: 0.0),
- tallLabel.bottomAnchor.constraint(equalTo: cg.bottomAnchor, constant: -8.0),
- tallLabel.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: -16.0),
- ])
- let anchorView = UILabel()
- anchorView.backgroundColor = .yellow
- anchorView.textAlignment = .center
- anchorView.text = "Anchor View"
- let constrainedView = UIImageView()
- if let img = UIImage(named: "sampleImage") {
- constrainedView.image = img
- }
- anchorView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(anchorView)
- constrainedView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(constrainedView)
- exampleConstraint = constrainedView.bottomAnchor.constraint(equalTo: anchorView.topAnchor, constant: 0.0)
- exampleConstraint.priority = .required - 1
- NSLayoutConstraint.activate([
- anchorView.leadingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
- anchorView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
- anchorView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0),
- anchorView.heightAnchor.constraint(equalToConstant: 40.0),
- exampleConstraint,
- constrainedView.topAnchor.constraint(greaterThanOrEqualTo: scrollView.topAnchor),
- constrainedView.leadingAnchor.constraint(equalTo: anchorView.leadingAnchor, constant: 0.0),
- constrainedView.trailingAnchor.constraint(equalTo: anchorView.trailingAnchor, constant: 0.0),
- constrainedView.heightAnchor.constraint(equalToConstant: 120.0),
- ])
- scrollView.delegate = self
- }
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- let up = scrollView.contentOffset.y
- print("up \(up)")
- exampleConstraint.constant = -up
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement