ios - UILabel 的自动滚动功能

标签 ios animation uiscrollview

我用谷歌搜索它,但在互联网上没有找到任何东西来解决我的问题,但我认为我的问题会非常容易解决,但我找不到合适的关键字。

我有一个 View Controller ,其中包含一个名为 ReadingView 的文本,该 View 具有很长的文本,可以滚动阅读文本。当用户进入 ReadingView 时,它会自动向下滚动到底部,用户可以通过我实现的 UISlider 提高滚动速度

问题是我可以通过这段代码毫无问题地自动滚动

 [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
 [UIScrollView setAnimationDuration:1.0f];
 [scroll setContentOffset:CGPointMake(x, y)];
 [UIScrollView commitAnimations];

但是上面的代码是基于时间的。我必须找到基于帧或像素的动画。因为,有些文本很长,如果我使用基于时间的动画,它会非常非常快地向下滚动到底部,而短文本会非常慢。

你们知道我是否可以做像这样的动画吗

"Hey scrollview, every FRAME you have to move down by 10 pixel until the end of context and i don't care how long does it take, Fixed the speed!!"

谢谢你的帮助,你们可以给我关键字或任何东西来帮助我

更新 由 Fogmeister 推荐完成。 这个概念是使用 block 动画并通过使用计算每个文本的时间 时间=文本长度/速度 并且您将获得每个文本的不同时间并使用该时间来制作动画。我从没想过这个....我一直在寻找的是通过 PIXEL 做动画的功能,这是一个错误的关键字。

CGFloat scrollingPointSpeed = myslider.value; //value of UI Slider//
CGFloat time = offset/scrollingPointSpeed ;
[UIView animateWithDuration:time delay:0.0 options:UIViewAnimationOptionCurveLinear animation:^()
{
 self.scroll.contentOffset = CGPointMake(0, offset);
}
completion:nil];

最佳答案

好的,首先,您不应该使用 CABasicAnimation。最佳实践方法是使用基于 block 的动画。

其次,这应该相当容易。

您可以计算 ScrollView 的最大偏移...

CGFloat maxOffsetWidth = self.scrollView.contentSize.width - CGRectGetWidth(self.scrollView.frame);

这是可以应用的最大滚动量。即距离。

现在,要计算持续时间(时间),您需要考虑速度。比方说每秒 50 点。

现在让我们把它放在一起......

CGFloat maxOffsetWidth = self.scrollView.contentSize.width - CGRectGetWidth(self.scrollView.frame);

CGFloat scrollingPointsPerSecond = 50.0;

CGFloat time = maxOffsetWidth / scrollPointsPerSecond;

[UIView animateWithDuration:time
                 animations:^() {
    self.scrollView.contentOffset = CGPointMake(maxOffsetWidth, 0);
}];

显然,这是用于水平滚动,如果您想要垂直滚动,则只需使用不同的值即可。

快速编辑

上面的动画函数使用了默认的动画曲线UIAnimationOptionCurveEaseInOut。这将意味着滚动的速度将从开始时的 0 缓慢加速,然后在结束时缓慢减速回到 0。

您可能想要的是线性运动。即滚动的速度始终相同。为此,您需要一个与上述函数略有不同的函数。它只是添加了几个额外的参数...

[UIView animateWithDuration:time
                      delay:0.0
                    options:UIViewAnimationOptionCurveLinear
                  animation:^() {
                      self.scrollView.contentOffset = CGPointMake(maxOffsetWidth, 0);
                  }
                 completion:nil];

这将完全按照您的要求进行线性运动。

关于ios - UILabel 的自动滚动功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764111/

相关文章:

ios - 在 UIPageControl 中为点添加边框

javascript - react : mount/unmount animation of component with variable height

javascript - 使用 JavaScript 将图像从 A 移动到 B

ios - UIWebView被键盘隐藏

iPhone:在设置导航栏显示/隐藏动画时无法设置 contentInset 动画

ios - 使用 map() 从字典数组(即 JSON 数组)初始化数组内联

html - 溢出-x : auto on iPhone is not scrolling

ios - 在 iOS 中加密数据

javascript - 为什么在主进程和渲染进程中调用的 window.maximize() 与 macos 上的动画不同?

ios - 在不支持旋转的iOS应用程序中旋转UIImageView