可以修改UIScrollView的减速率吗?

时间:2009-02-04 21:18:25

标签: cocoa-touch

由于最初询问此问题,因此已通过OS 3.0中引入的decelerationRate property添加了UIScrollView减速率自定义。


我有一个UIScrollView,其减速率我想改变。为了清楚起见,我说的是当你在滚动视图上滑动手指并且在抬起手指后视图继续滚动(但逐渐减慢)。我想提高减速率,以便它在默认情况下更快停止。

我见过一些UIScrollViews看起来速度更快的应用程序。在UIScrollView中似乎没有这个API,但我想知道是否有另一种方法可以做到这一点。

5 个答案:

答案 0 :(得分:47)

您可以使用UIScrollViewdecelerationRate属性来控制它。即使它的浮动,它不接受除UIScrollViewDecelerationRateNormal或UIScrollViewDecelerationRateFast 之外的任何值。请查看以下代码

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);

上面的代码给出了以下输出,很明显我们不能使用自定义减速率。

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

答案 1 :(得分:35)

是的,我通过以下方式成功改变了减速率:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

答案 2 :(得分:13)

我发现通过使用KVC修改实例变量_decelerationFactor,我可以将速率更改为UIScrollViewDecelerationRateNormalUIScrollViewDecelerationRateFast以外的其他值。我将UIScrollView子类化,并将所有内容包装在试块中

- (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;
}

答案 3 :(得分:1)

有一种非常简单的方法。覆盖setter方法。

@interface UIArbitraryDeceleratingScrollView : UIScrollView

@property(nonatomic,assign) CGFloat decelerationRate;

@end

@implementation UIArbitraryDeceleratingScrollView

@synthesize decelerationRate = _decelerationRate;

- (void)setDecelerationRate:(CGFloat)dr
{
    [super setDecelerationRate:dr];
    _decelerationRate = dr;
}

@end

现在分配您想要的内容,例如:

_scroller.decelerationRate = (UIScrollViewDecelerationRateNormal+UIScrollViewDecelerationRateFast)/2.1;

答案 4 :(得分:0)

这是可能的,但您必须继承UIScrollView并覆盖所有触摸处理。这不是我的选择项目 - 要做到这一点需要做很多工作。