ios - 我可以通过其图层为 UIScrollView contentOffset 属性设置动画吗?

标签 ios uiscrollview core-animation cakeyframeanimation

我想使用 CGPathRef 缩放和滚动 UIScrollView。因此我假设我必须为 UIScrollView 的图层属性设置动画?但是我应该为哪个属性设置动画,使其等同于执行 UIView 动画并设置其 contentOffset 属性和 zoomScale ?

这些不是 CALayer 的属性。

关于我将如何处理这个问题有什么想法吗?同样,只是想将scrollview移动到某个contentOffset和zoomScale,但不一定是从A点线性移动到B点,分别缩放A到缩放B。

我正在考虑使用 CGPathRef 的 CAKeyFrameAnimation,但我不知道要为哪些属性设置动画。

最佳答案

您必须为 bounds 设置动画属性(property)。事实上,这就是 contentOffset属性(property)在幕后使用。

例子:

CGRect bounds = scrollView.bounds;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.duration = 1.0;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.fromValue = [NSValue valueWithCGRect:bounds];

bounds.origin.x += 200;

animation.toValue = [NSValue valueWithCGRect:bounds];

[scrollView.layer addAnimation:animation forKey:@"bounds"];

scrollView.bounds = bounds;

如果你很好奇,我以前获取这些信息的方式如下:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

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

[UIView commitAnimations];

NSLog(@"%@",scrollView);

NSLog调用将输出:

<UIScrollView: 0x860ba20; frame = (-65.5 0; 451 367); clipsToBounds = YES; autoresize = W+RM+TM+H; animations = { bounds=<CABasicAnimation: 0xec1e7c0>; }; layer = <CALayer: 0x860bbc0>; contentOffset: {246, 0}>

animations片段将列出所有事件的动画,在本例中为 { bounds=<CABasicAnimation: 0xec1e7c0>; } .

希望这对您有所帮助。

关于ios - 我可以通过其图层为 UIScrollView contentOffset 属性设置动画吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6535450/

相关文章:

ios - 从每个月的第 1 天开始倒计时

iOS:一次滚动一个单元格

ios - UIScrollView 一个分页器

ios - 更改 UICollectionView 大小而不使用 viewDidLayoutSubviews

php - iOS 推送通知发送错误

ios - 如果应用程序断开互联网,如何再次获取设备 token (APNS)?

ios - 以编程方式将 UIScrollView 添加到 UIView 在 vi​​ewDidLoad 中不起作用,但在 viewDidAppear 中起作用?

ios - 如何使 CALayer 可点击?

iphone - iOS图标抖动算法

ios - 使用 CABasicAnimation 动画框架属性