What I want to get is the same behaviour that this scroll view has:
I know that this is using HTML and not the native API, but I'm trying to implement it as a UIKit component.
Now, to the behaviour I'm looking for:
The same page but now right-to-left:
What I've tried:
I'm out of ideas.
Thanks!
Update:
I ended up using Rob Mayoff's method, it looks clean. I changed it so it would work when the velocity is 0, for example when a user drags, stops and releases the finger.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(CGPoint *)targetContentOffset {
CGFloat maxOffset = scrollView.contentSize.width - scrollView.bounds.size.width;
CGFloat minOffset = 0;
if (velocity.x == 0) {
CGFloat targetX = MAX(minOffset,MIN(maxOffset, targetContentOffset->x));
CGFloat diff = targetX - baseOffset;
if (ABS(diff) > offsetStep/2) {
if (diff > 0) {
//going left
baseOffset = MIN(maxOffset, baseOffset + offsetStep);
} else {
//going right
baseOffset = MAX(minOffset, baseOffset - offsetStep);
}
}
} else {
if (velocity.x > 0) {
baseOffset = MIN(maxOffset, baseOffset + offsetStep);
} else {
baseOffset = MAX(minOffset, baseOffset - offsetStep);
}
}
targetContentOffset->x = baseOffset;
}
The only problem with this solution is that swiping the scroll view doesn't produce the "bounce" effect. It feels "stuck".
Setting scrollView.decelerationRate = UIScrollViewDecelerationRateFast
, combined with implementing scrollViewWillEndDragging:withVelocity:targetContentOffset:
, seems to work for me using a collection view.
First, I give myself some instance variables:
@implementation ViewController {
NSString *cellClassName;
CGFloat baseOffset;
CGFloat offsetStep;
}
In viewDidLoad
, I set the view's decelerationRate
:
- (void)viewDidLoad {
[super viewDidLoad];
cellClassName = NSStringFromClass([MyCell class]);
[self.collectionView registerNib:[UINib nibWithNibName:cellClassName bundle:nil] forCellWithReuseIdentifier:cellClassName];
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
}
I need offsetStep
to be the size of an integral number of items that fit in the view's on-screen bounds. I compute it in viewDidLayoutSubviews
:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
CGFloat stepUnit = layout.itemSize.width + layout.minimumLineSpacing;
offsetStep = stepUnit * floorf(self.collectionView.bounds.size.width / stepUnit);
}
I need baseOffset
to the be the X offset of the view before scrolling starts. I initialize it in viewDidAppear:
:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
baseOffset = self.collectionView.contentOffset.x;
}
Then I need to force the view to scroll in steps of offsetStep
. I do that in scrollViewWillEndDragging:withVelocity:targetContentOffset:
. Depending on the velocity
, I increase or decrease baseOffset
by offsetStep
. But I clamp baseOffset
to a minimum of 0 and a maximum of the contentSize.width - bounds.size.width
.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (velocity.x < 0) {
baseOffset = MAX(0, baseOffset - offsetStep);
} else if (velocity.x > 0) {
baseOffset = MIN(scrollView.contentSize.width - scrollView.bounds.size.width, baseOffset + offsetStep);
}
targetContentOffset->x = baseOffset;
}
Note that I don't care what targetContentOffset->x
comes in as.
This has the effect of aligning to the left edge of the leftmost visible item, until the user scrolls all the way to the last item. At that point it aligns to the right edge of the rightmost visible item, until the user scroll all the way to the left. This seems to match the behavior of the App Store app.
If that doesn't work for you, you can try replacing the last line (targetContentOffset->x = baseOffset
) with this:
dispatch_async(dispatch_get_main_queue(), ^{
[scrollView setContentOffset:CGPointMake(baseOffset, 0) animated:YES];
});
That also works for me.
You can find my test app in this git repository.
You can either turn on pagingEnabled
, or use decelerationRate = UIScrollViewDecelerationRateFast
combined with scrollViewDidEndDragging
and scrollViewDidEndDecelerating
(which will minimize the effect of slowing scrolling to one location and then animating again to another location). It's probably not precisely what you want, but it's pretty close. And by using animateWithDuration
it avoids the instantaneous jumping of that final snap.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
[self snapScrollView:scrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self snapScrollView:scrollView];
}
You can then write a snapScrollView
, such as:
- (void)snapScrollView:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
if ((offset.x + scrollView.frame.size.width) >= scrollView.contentSize.width)
{
// no snap needed ... we're at the end of the scrollview
return;
}
// calculate where you want it to snap to
offset.x = floorf(offset.x / kIconOffset + 0.5) * kIconOffset;
// now snap it to there
[UIView animateWithDuration:0.1
animations:^{
scrollView.contentOffset = offset;
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With