Your storyboard has autolayout enabled. When autolayout runs, it sets the frames of your views based on the constraints you set up (or that were automatically set up for you) in the storyboard. This means that if you change the frame of a view, autolayout will undo your change the next time it runs.
The system triggers autolayout automatically at various times, including when you scroll a scroll view. That's why you're seeing your view “jump back” when you scroll.
If you don't need autolayout, you can turn it off (see this answer if you need help turning it off). Then you can use the older “springs and struts” layout system in your storyboard, which lets you set the frames of your views however you want.
If you do need autolayout, you need to learn how to make animations work with autolayout. There are two basic approaches. One approach is to modify the constraints so that autolayout will put the view where you want, and then do [view layoutIfNeeded]
in an animation block to animate the view to its new position. The other approach is to animate the constraints themselves using an NSTimer
or a CADisplayLink
.
The WWDC 2012 video “Session 228 - Best Practices for Mastering Auto Layout” explains your problem, and discusses these two techniques in depth, starting at 30m45s. If you need to use animation with autolayout, I highly recommend you watch the video, and the other autolayout-related videos from WWDC 2012.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…