由于最初询问此问题,因此通过OS 3.0中引入的decelerationRate属性添加了UIScrollView减速率自定义.
我有一个UIScrollView,其减速率我想改变.为了清楚起见,我说的是当你在滚动视图上滑动手指并且在抬起手指后视图继续滚动(但逐渐减慢).我想提高减速率,以便它在默认情况下更快停止.
我见过一些UIScrollViews似乎减速更快的应用程序.在UIScrollView中似乎没有这个API,但我想知道是否有另一种方法可以做到这一点.
您可以使用UIScrollView的decelerationRate属性来控制它.即使它是浮点数,它也不接受除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
是的,我通过以下方式成功改变了减速率:
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
我发现通过使用KVC修改实例变量_decelerationFactor
允许我将速率更改为除UIScrollViewDecelerationRateNormal
or之外的其他值UIScrollViewDecelerationRateFast
.我将一小部分子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; }