25

我想用 CGPathRef 缩放和滚动 UIScrollView。因此,我假设我必须为 UIScrollView 的 layer 属性设置动画?但是我会为哪个属性设置动画,使它相当于做一个 UIView 动画并设置它的 contentOffset 属性和 zoomScale ?

这些不是 CALayer 的属性。

关于我将如何处理这个问题的任何想法?再次,只是想将滚动视图移动到某个 contentOffset 和 zoomScale,但不一定是从 A 点到 B 点,分别从 A 点到 B 点进行线性缩放。

我在想一个带有 CGPathRef 的 CAKeyFrameAnimation,但我不知道要为哪些属性设置动画。

4

3 回答 3

59

您必须为bounds属性设置动画。事实上,这就是该contentOffset物业在幕后使用的东西。

例子:

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>; }

希望这可以帮助。

于 2012-01-05T10:53:46.260 回答
0

移动 CALayer 是通过(最好)设置它的 .position 属性 - 或者可能是 anchorPoint (参见上面的文档:http: //developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CALayer_class/简介/Introduction.html )。

...但是如果您使用 UIScrollView,我认为您不想弄乱 CALayer。您是否尝试过将普通的 CoreAnimations 应用到 ScrollView?

(问题是:UIScrollView 是在 CALayer 之上实现的——所以即使你今天可以破解它来工作,它也很可能在未来的 iOS 版本中中断。如果可能,你想避免该特定类的 CALayer)

于 2011-10-09T22:32:21.990 回答
0

斯威夫特 4.2

这个例子是基于 pt2ph8 的obj-c回答。

https://stackoverflow.com/a/8741283/6113158

var scrollView = UIScrollView()

func animateScrollView(duration: Double, to newBounds: CGRect) {
    let animation = CABasicAnimation(keyPath: "bounds")
    animation.duration = duration
    animation.fromValue = scrollView.bounds
    animation.toValue = newBounds

    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)

    scrollView.layer.add(animation, forKey: nil)

    scrollView.bounds = newBounds
}
于 2019-03-21T08:45:30.700 回答