循环 UIScrollView 但继续减速

Loop UIScrollView but continue decelerating(循环 UIScrollView 但继续减速)

本文介绍了循环 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.

[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?

我试过了……

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

但是没有用.

编辑

我刚刚意识到 decelerationRate 是用于确定减速的慢/快的设置.

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

我需要的是从滚动视图中获取当前速度.

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

推荐答案

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

Right I had to tweak the idea a bit.

原来尝试设置 UIScrollView 的速度很困难……非常困难.

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

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

So anyway, I kind of tweaked it.

这实际上是一个迷你项目,在回答了别人的 SO 问题并认为我会尝试自己解决之后.

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.

然后在代码中...

@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 但继续减速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:循环 UIScrollView 但继续减速

基础教程推荐