BillCarsonFr / ScrollviewParallax

Illustration of Pete Share's post on domesticcat.com
78 stars 10 forks source link

Let imageView scroll #1

Open onmyway133 opened 8 years ago

onmyway133 commented 8 years ago

Thanks for the code, you explain better than the original blog :D

With the current implementation, the imageView always pins to the top. Now we have 2 top constraints from the imageView to the TopLayoutGuide (1 with equal, priority 999 and 1 with less than, priority 1000), let call them C1 and C2

This would make it scroll with the scrollView as well

func scrollViewDidScroll(scrollView: UIScrollView) {
        let y = scrollView.contentOffset.y

        if y > 0 {
            c1.constant = -y
            c2.constant = -y
        } else {
            c1.constant = 0
            c2.constant = 0
        }
    }
onmyway133 commented 8 years ago

I tried putting a heightConstraint on the imageView, and try activate, deactivate those 3 constraints (heightConstraint, C1, C2) but it crashes

func scrollViewDidScroll(scrollView: UIScrollView) {
        let sketchy = scrollView.contentOffset.y <= 0

        heightConstraint.active = !sketchy
        c1.active = sketchy
        c2.active = sketchy
    }