4

I'm having a problem with UIScrollView, not sure if it's a bug or not, but it's occurring when I implement a UIScrollView with its delegate and a zoomable / pannable image.

First off, when I pan the image, it's possible that the contentOffset can be a non integer value (.5). For certain zoomScales, when I pan the image all the way to the edge, it's falling a half pixel shy of reaching the edge because of this.

It's a very small thing, but in my app I have to drag objects around the screen, and if I drag an object to the corner, you can notice it.

I've implemented the following to try and correct the problem and make it so the contentOffset has to be a whole value:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (targetContentOffset->x != ceilf(targetContentOffset->x))
    {
        if (velocity.x > 0)
        {
            targetContentOffset->x = ceilf(targetContentOffset->x);
        }
        else 
        {
            targetContentOffset->x = floorf(targetContentOffset->x);
        }
    }

    if (targetContentOffset->y != ceilf(targetContentOffset->y))
    {
        if (velocity.y > 0)
        {
            targetContentOffset->y = ceilf(targetContentOffset->y);
        }
        else 
        {
            targetContentOffset->y = floorf(targetContentOffset->y);
        }
    }
}

However, it doesn't seem to be working as my targetContentOffset is completely different than the contentOffset property.

Does anyone a) why this bug is occurring or b) how to fix it using the above delegate method or some other means?

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169
  • Hmm, you're right. This seems like it should work. I deleted my answer, and learned something new in the process. – Christian Jun 01 '12 at 22:13

1 Answers1

2

Alternatively this method implementation may work for you also.

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

Looking at your comments below, you may have to implement multiple methods. The one above or the one stated in the other answer post for when it slows to a stop, just to make sure you touch your edge condition, and the method you already have to process for the velocity points.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • Also try scrollViewDidEndDecelerating if necessary, and/or scrollViewDidScroll...just set the content offset to a new value, not animated. – Matt Jun 01 '12 at 23:44