I'm using KVO
to observe the the contentOffset
property of a UIScrollView
. When the user is pulling the scrollView
down and the contentOffset.y == -50.0
, I want the scroll view to release as if the user took their finger off the screen. Is there anyway to do this?
Asked
Active
Viewed 314 times
0

coder
- 10,460
- 17
- 72
- 125

Darren Findlay
- 2,533
- 2
- 29
- 46
1 Answers
0
Yes, you can limit it's offset to -50.0.
- (void) observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
//if this is not the only KVO then you should first perform some checks
//if the object and keypath are correct
//otherwise you can omit the check or modify, if your UIScrollView is subclassed
if ([object isKindOfClass:[UIScrollView class]])
{
UIScrollView *scrl = (UIScrollView *)object;
CGPoint offset = scrl.contentOffset;
if (offset.y < -50.0f)
{
offset.y = -50.0f;
scrl.contentOffset = offset;
}
}
}
You might want to consider moving this code in scrollView
delegate methods (didScroll:
and others. KVO is a great tool but it might be too expensive for rapidly changing values.

Rok Jarc
- 18,765
- 9
- 69
- 124
-
Thanks for the reply but unfortunately setting the scrollview's contentOffset property doesn't make the scrollview act like the user let go of it. I just tried it out there. – Darren Findlay Apr 26 '13 at 16:51
-
1Hmm, maybe i understood you wrong. So you want the scrollView to keep scrolling just as if user lifted his finger? In that case i'd try setting `touchesEnabled` to `NO` when `y` reaches -50.0, but you would also have to implement a delegate method `didEndScrollingAnimation`, where you would reenable touches for that `scrollView`: I'd definetly move all the behaviour related code to `delegate` methods and ditch the KVO in this particular case. – Rok Jarc Apr 26 '13 at 18:33