
You can add a spring bounce to the content in your scroll, for example:
Customize the UIScrollview view and add it to the view.
mainScroller = [UIScrollView new]; mainScroller.frame = CGRectMake(0, 0, w, h); mainScroller.bounces = true; mainScroller.pagingEnabled = false; mainScroller.delegate = self; [self.view addSubview:mainScroller];
Place a UIView and add it to your scrollview.
contentView = [UIView new]; contentView.frame = CGRectZero; [mainScroller addSubview:contentView];
Add subviews of your "contentView".
UIView * unitView = [UIView new]; unitView.frame = CGRectMake(0, yOff, w, 280); [contentView addSubview:unitView];
Resize both the contentView and scrollview to fit the content. Below w is the width of the view, and yOff is the total cumulative height of the content.
contentView.frame = CGRectMake(0, 0, w, yOff); mainScroller.contentSize = CGSizeMake(w, yOff);
In your scrollViewDidScroll method add the following code:
float y = mainScroller.contentOffset.y; contentView.transform = CGAffineTransformMakeTranslation(0, y); for (UIView * sub in contentView.subviews){ int index = (int)[contentView.subviews indexOfObject:sub]; float delay = index * 0.03; float duration = 1.0f - delay; [UIView animateWithDuration:duration delay:delay usingSpringWithDamping:0.8 initialSpringVelocity:0.7 options:UIViewAnimationOptionCurveEaseInOut| UIViewAnimationOptionAllowUserInteraction animations:^{ sub.transform = CGAffineTransformMakeTranslation(0, -y); } completion:^(BOOL finished){ }]; }
The UIViewAnimationOptionAllowUserInteraction parameter allows you to interact with the content immediately. Change the delay, duration, spring humidification and spring speed variables to suit your needs.
The code can be further adapted to provide sensory detection; as it stands, a springy rebound occurs at the top of the scrollView and goes down through the views, which for shorter scrollViews are hardly noticeable.
EDIT: touch detection
If you want to enable touch detection, replace these lines with your scrollViewDidScroll method:
int index = (int)[contentView.subviews indexOfObject:sub]; int deviation = abs(touchIndex - index); float delay = deviation * 0.03; float duration = 1.0f - delay;
Where the new touchIndex variable is defined as follows:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
If you have dynamic cell heights, store them in an array, for example. 'selectedHeights'. Then update your scrollViewWillBeginDragging method to below, where the 'tally' float parameter is set to 70 to enable the title.
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //grab the location of the touch event CGPoint location = [scrollView.panGestureRecognizer locationInView:scrollView]; int yTouch = location.y; //grab y coordinate float tally = 70.0f; for (int n = 0; n < selectedHeights.count; n++){ float cellHeight = [selectedHeights[n] floatValue]; tally += cellHeight; if (tally > yTouch){ touchIndex = n; break; } } }
Johnny Rockex Feb 02 '16 at 22:24 2016-02-02 22:24
source share