MWPhotoBrowser icon indicating copy to clipboard operation
MWPhotoBrowser copied to clipboard

Custom CaptionView with Autolayout causes stutter/jerks while scrolling slowly

Open jquiambao opened this issue 9 years ago • 6 comments

I subclassed MWCaptionView that has 4 buttons and used AutoLayout to handle orientation change.

I initialize by code via [[NSBundle mainBundle] loadNibNamed:] and return it from captionViewForPhotoAtIndex:. When swiping through the image slowly, the view would jerk. Swiping it quickly would work fine.

Initially, I thought it was the loadNib was too slow, but then I cached my object (and tried UNib) and it didn't make a difference. I unchecked autolayout from my xib file and it worked smoothly.

jquiambao avatar Oct 20 '15 21:10 jquiambao

I have the same stuttering even without subclassing MWCaptionView. If I don't provide any captions, there is no stuttering.

markkrenek avatar Oct 28 '15 15:10 markkrenek

For me, removing the call to layoutVisiblePages in viewWillLayoutSubviews resolved my issue. I can't explain exactly why, but here's what I'm seeing: During a scroll, layout happens once via a call to tilePages, but then viewWillLayoutSubviews also gets called which triggers another layout pass. Between these two, something causes the contentOffset of the scrolling view to get reset to a page boundary, which you don't want when in the middle of a scroll (hence the stutter).

While it seems to make sense to layout the view when viewWillLayoutSubviews is called, I can't find any negative consequence from skipping it. Rotation still works.

markkrenek avatar Oct 28 '15 18:10 markkrenek

Another solution that seems to work is to NOT change the contentOffset at the end of layoutVisiblePages if the user is scrolling the view.

Change:

_pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:indexPriorToLayout];

to:

if (!_pagingScrollView.dragging && !_pagingScrollView.decelerating)
    _pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:indexPriorToLayout];

I'm not really sure which route is best. There may be some other interactions going on that I'm not aware.

markkrenek avatar Oct 28 '15 19:10 markkrenek

cool,i just know that:viewWillLayoutSubviews called ,so that" _pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:indexPriorToLayout]; "change the offset,result in jerks.but i dont know why viewWillLayoutSubviews is called ,in MWPhoto demo,the method will not be called.so i choose the second route to solve my problem,thanks

xiongyoudou avatar May 30 '16 03:05 xiongyoudou

@markkrenek It works. Thanks!

xilin avatar Oct 10 '16 10:10 xilin

@markkrenek thanks it works.

TianXianBob avatar Oct 07 '18 09:10 TianXianBob