循环UIScrollView但继续减速 [英] Loop UIScrollView but continue decelerating

查看:152
本文介绍了循环UIScrollView但继续减速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've set up an infinite scroll view and when it reaches 0 content offset I set it to max content offset and vice versa.

ie

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

这样可以阻止UIScrollView减速。

This works but it stops the UIScrollView decelerating.

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

Is there a way to do this but keep the UIScrollView decelerating?

我试过这个......

I tried this...

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

但它不起作用。

编辑

我刚刚意识到,减速率是确定减速速度慢/快的设置。

I just realised that decelerationRate is the setting to determine how slow/fast the deceleration is.

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

What I need is to get the current velocity from the scrollView.

推荐答案

我必须调整这个想法。

结果试图设置UIScrollView的速度很困难......非常困难。

Turns out trying to set the velocity of a UIScrollView is difficult... very difficult.

所以无论如何,我有点调整它。

So anyway, I kind of tweaked it.

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

This is actually a mini project after answering someone else's SO question and thought I'd try to solve it myself.

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

I want to create a spinner app that I can swipe to spin an arrow around so it spins and decelerates to a point.

我所做的是设置一个UIImageView,箭头指向上方。

What I did was set up a UIImageView with the arrow pointing up.

然后覆盖UIImageView是一个UIScrollView。

Then covering the UIImageView is a UIScrollView.

然后在代码中......

Then in the code...

@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

这给出了所需的效果像命运之轮上的旋转器会在任何一个方向上无休止地旋转。

This gives the desired effect of a spinner like on Wheel of Fortune that will spin endlessly in either direction.

但是有一个缺陷。如果用户在不让它停止的情况下继续旋转和旋转,它将在停止之前向任一方向旋转50次。

There is a flaw though. If the user keeps spinning and spinning without letting it stop it will only go 50 spins in either direction before coming to a stop.

这篇关于循环UIScrollView但继续减速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆