Can UIScrollView's deceleration rate be modified?

前端 未结 5 692
失恋的感觉
失恋的感觉 2020-12-14 01:36

Since this question was originally asked, UIScrollView deceleration rate customization has been added via the decelerationRate property introduced in OS 3.0.

相关标签:
5条回答
  • You can use UIScrollView's decelerationRate property to control it. Even though its float, its not accepting any value other than UIScrollViewDecelerationRateNormal or UIScrollViewDecelerationRateFast . Look at the following code

    NSLog(@"1. decelerationRate %f", scrollview.decelerationRate);
    
    scrollview.decelerationRate = UIScrollViewDecelerationRateNormal;
    NSLog(@"2. decelerationRate %f", scrollview.decelerationRate);
    
    scrollview.decelerationRate = UIScrollViewDecelerationRateFast;
    NSLog(@"3. decelerationRate %f", scrollview.decelerationRate);
    
    scrollview.decelerationRate = 0.7;
    NSLog(@"4. decelerationRate %f", scrollview.decelerationRate);
    
    scrollview.decelerationRate = 0.995;
    NSLog(@"5. decelerationRate %f", scrollview.decelerationRate);
    

    Above code gives the following outputs, its very clear we cant not use custom deceleration rate.

    2012-01-03 11:59:41.164 testviewv2[10023:707] 1. decelerationRate 0.998000
    2012-01-03 11:59:41.172 testviewv2[10023:707] 2. decelerationRate 0.998000
    2012-01-03 11:59:41.173 testviewv2[10023:707] 3. decelerationRate 0.990000
    2012-01-03 11:59:41.175 testviewv2[10023:707] 4. decelerationRate 0.990000
    2012-01-03 11:59:41.176 testviewv2[10023:707] 5. decelerationRate 0.998000
    
    0 讨论(0)
  • 2020-12-14 02:18

    This is possible, but you'd have to subclass UIScrollView and override all the touch handling. It wouldn't be my project of choice - it would take a good bit of work to get it right.

    0 讨论(0)
  • 2020-12-14 02:21

    There is a pretty straightforward approach. Override the setter method.

    @interface UIArbitraryDeceleratingScrollView : UIScrollView
    
    @property(nonatomic,assign) CGFloat decelerationRate;
    
    @end
    
    @implementation UIArbitraryDeceleratingScrollView
    
    @synthesize decelerationRate = _decelerationRate;
    
    - (void)setDecelerationRate:(CGFloat)dr
    {
        [super setDecelerationRate:dr];
        _decelerationRate = dr;
    }
    
    @end
    

    Now assign what you want to, like this for example:

    _scroller.decelerationRate = (UIScrollViewDecelerationRateNormal+UIScrollViewDecelerationRateFast)/2.1;
    
    0 讨论(0)
  • 2020-12-14 02:24

    Yes, I have successfully changed the deceleration rate by doing the following:

    scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    
    0 讨论(0)
  • 2020-12-14 02:29

    I found that by using KVC to modify the instance variable _decelerationFactor allowed me to change the rate to something other than UIScrollViewDecelerationRateNormal or UIScrollViewDecelerationRateFast. I subclassed UIScrollView and wrapped the whole lot in a try block

    - (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;
    }
    
    0 讨论(0)
提交回复
热议问题