gpt4 book ai didi

iphone - 循环 UIScrollView 但继续减速

转载 作者:可可西里 更新时间:2023-11-01 05:45:42 25 4
gpt4 key购买 nike

我设置了一个无限 ScrollView ,当它达到 0 内容偏移量时,我将它设置为最大内容偏移量,反之亦然。

[scrollView setContentOffset:CGPointMake(0,0) animated:NO];

这有效,但它会阻止 UIScrollView 减速。

有没有办法做到这一点,但保持 UIScrollView 减速?

我试过了...

float declerationRate = scrollView.decelerationRate;
[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
scrollView.decelerationRate = declerationRate;

但是没用。

编辑

我刚刚意识到 decelerationRate 是确定减速有多慢/多快的设置。

我需要的是从 scrollView 获取当前速度。

最佳答案

是的,我不得不稍微调整一下这个想法。

事实证明,尝试设置 UIScrollView 的速度很困难……非常困难。

所以无论如何,我对它进行了一些调整。

这实际上是在回答了别人的 SO 问题之后的一个小项目,我想我会尝试自己解决它。

我想创建一个微调器应用程序,我可以滑动它来旋转箭头,使其旋转并减速到一个点。

我所做的是设置一个箭头朝上的 UIImageView。

然后覆盖 UIImageView 的是一个 UIScrollView。

然后在代码中...

@interface MyViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property (nonatomic, weak) IBOutlet UIImageView *arrowView;

@end

@implementation MyViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//make the content size really big so that the targetOffset of the deceleration will never be met.
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 100, self.scrollView.frame.size.height);
//set the contentOffset of the scroll view to a point in the center of the contentSize.
[self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * 50, 0) animated:NO];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)rotateImageView
{
//Calculate the percentage of one "frame" that is the current offset.

// each percentage of a "frame" equates to a percentage of 2 PI Rads to rotate
float minOffset = self.scrollView.frame.size.width * 50;
float maxOffset = self.scrollView.frame.size.width * 51;

float offsetDiff = maxOffset - minOffset;

float currentOffset = self.scrollView.contentOffset.x - minOffset;

float percentage = currentOffset / offsetDiff;

self.arrowView.transform = CGAffineTransformMakeRotation(M_PI * 2 * percentage);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//the scrollView moved so update the rotation of the image
[self rotateImageView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//the scrollview stopped moving.
//set the content offset back to be in the middle
//but make sure to keep the percentage (used above) the same
//this ensures the arrow is pointing in the same direction as it ended decelerating

float diffOffset = scrollView.contentOffset.x;

while (diffOffset >= scrollView.frame.size.width) {
diffOffset -= scrollView.frame.size.width;
}

[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * 50 + diffOffset, 0) animated:NO];
}

@end

这提供了像命运之轮上的旋转器的预期效果,它会在任一方向上无限旋转。

虽然有一个缺陷。如果用户不停地旋转和旋转而不让它停下来,它只会在任一方向旋转 50 圈才停止。

关于iphone - 循环 UIScrollView 但继续减速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14632754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com