Tuesday, June 4, 2013

UIScrollView decelerationRate example in Objective C (iOS).


UIScrollView decelerationRate

A floating-point value that determines the rate of deceleration after the user lifts their finger.

@property(nonatomic) float decelerationRate

Discussion of [UIScrollView decelerationRate]
Your application can use the UIScrollViewDecelerationRateNormal and UIScrollViewDecelerationRateFast constants as reference points for reasonable deceleration rates.

UIScrollView decelerationRate example.
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        @try {
            CGFloat decelerationRate = UIScrollViewDecelerationRateFast +(UIScrollViewDecelerationRateNormal - UIScrollViewDecelerationRateFast) * .52;
            [self setValue:[NSValue valueWithCGSize:CGSizeMake(decelerationRate,decelerationRate)] forKey:@"_decelerationFactor"];

        }
        @catch (NSException *exception) {
            // if they modify the way it works under us.
        }

    }
    return self;
}

Example of [UIScrollView decelerationRate].
You can just turn up the deceleration rate very high. With an infinite rate, it would stop immediately. Try setting the rate to these constants:

scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
and

scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

UIScrollView decelerationRate example.
In iOS 4/5, we can simply use the last subview of UIWebView.

UIScrollView *scroll = [webView.subviews lastObject];
if ([scroll isKindOfClass:[UIScrollView class]]) {
    scroll.decelerationRate = UIScrollViewDecelerationRateNormal;
}
The default decelerationRate of UIWebView's UIScrollView is 0.989324, while UIScrollViewDecelerationRateFast is 0.99, and UIScrollViewDecelerationRateNormal is 0.998.

End of UIScrollView decelerationRate example article.