我想使用 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/