Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Scroll direction pulses with a value sensitivity
       
back

@interface ViewController () <UIScrollViewDelegate>
{
    CGFloat initialContentOffset;
    CGFloat previousContentDelta;
}

@property (nonatomic, assign) CGFloat senseValue;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.senseValue = 300;
}

#pragma mark - UIScollView Delegates

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    initialContentOffset = scrollView.contentOffset.x;
    previousContentDelta = 0.f;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat prevDelta = previousContentDelta;
    CGFloat delta = scrollView.contentOffset.x - initialContentOffset;
    
    if (fabs(delta - prevDelta) >= self.senseValue) {
        if (delta > 0.f && prevDelta <= 0.f) {
            //
            // scroll from right to left
            //
        } else if (delta < 0.f && prevDelta >= 0.f) {
            //
            // scroll from left to right
            //
        }
        
        previousContentDelta = delta;
    }
}

@end






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com