問題描述
我設置了一個無限滾動視圖,當它達到 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 但繼續減速的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!