我可以通过其图层为UIScrollView contentOffset属性设置动画吗? [英] Can I animate the UIScrollView contentOffset property via its layer?
问题描述
我想用CGPathRef缩放和滚动UIScrollView。因此,我假设我必须为UIScrollView的图层属性设置动画?但是我会为哪个属性设置动画会使它等同于执行UIView动画并设置其contentOffset属性和zoomScale?
I want to zoom and scroll a UIScrollView with a CGPathRef. Because of that I assume I have to animate the UIScrollView's layer property? But which property would I animate that would make it equivalent to doing a UIView animation and setting its contentOffset property and zoomScale ?
这些不是CALayer的属性。
These are not properties of a CALayer.
关于如何处理这个问题的任何想法?再次,只是想将scrollview移动到某个contentOffset和zoomScale,但不一定是从A点到B点的线性移动,分别是缩放A到缩放B.
Any ideas as to how I would approach this? Again, just want to move the scrollview to a certain contentOffset and zoomScale, but not necessarily linearly from point A to point B, zoom A to zoom B, respectively.
I我正在考虑使用CGPathRef进行CAKeyFrameAnimation,但我不知道要设置动画的属性。
I was thinking a CAKeyFrameAnimation with a CGPathRef, but I don't know which properties to animate.
推荐答案
你必须为<动画<动画 bounds
属性。实际上,这就是 contentOffset
属性在幕后使用的内容。
You have to animate the bounds
property. In fact, that's what the contentOffset
property uses behind the scenes.
示例:
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;
如果您感到好奇,我以前获取此信息的方式如下:
If you're curious, the way I used to get this information is the following:
[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}>
动画
片段将列出所有活动动画,在这种情况下 {bounds =< CABasicAnimation:0xec1e7c0> ;; }
。
The animations
snippet will list all the active animations, in this case { bounds=<CABasicAnimation: 0xec1e7c0>; }
.
希望这会有所帮助。
这篇关于我可以通过其图层为UIScrollView contentOffset属性设置动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!