使用CoreAnimation动画滚动UIScrollView [英] Animate scrolling of UIScrollView with CoreAnimation

查看:379
本文介绍了使用CoreAnimation动画滚动UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉一个大问题,真正的问题是粗体在底部,现在有一些解释。



我在我的项目中使用CoreAnimation用于动画一些像这样移动的物体。

  CABasicAnimation * moveAnimation; 
moveAnimation = [CABasicAnimation animationWithKeyPath:@position];
moveAnimation.duration = 2.0;
moveAnimation.repeatCount = 1;
moveAnimation.autoreverses = NO;
moveAnimation.delegate = self;
moveAnimation.fromValue = [NSValue valueWithCGPoint:[crawler.layer position]];
moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.removedOnCompletion = NO;
CGPoint p;
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100,100)];
[crawler.layer addAnimation:moveAnimation forKey:@moveAnimation];

现在我需要动画滚动一些UIScrollView。我不能使用 setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated 方法,因为它无法自定义动画持续时间。



我可以这样做

  CGContextRef context = UIGraphicsGetCurrentContext(); 
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x + 200,scrollView.contentOffset.y);
[UIView commitAnimations];

但它不是CoreAnimation,在一个应用程序中使用2种不同的动画技术不是一个好习惯,我认为。



有没有办法使用CoreAnimation动画将UIScrollView内容滚动到具有自定义动画持续时间的自定义点?
PS抱歉英文不好。

解决方案

你应该阅读有关动画属性的内容,编程iOS 5



的确,当你尝试为图层设置动画时,Apple给出了一个动画属性集,但您不限于此集。你可以动画你想要的任何东西。



在所有情况下,你不应该使用 kCAFillModeForwards 因为它会询问设备永远重绘用于动画的最后一个表示层,而不是使用图层的缓存表示。



所以你可以实现这样的事情的方式通过定义您的特定图层,其中 contentOffset 属性被定义为可动画。为了工作,您应该首先验证scrollview使用哪种顶层类,以便通过子类化并设置所需属性( contentOffset )动画制作,并通过将此图层设置为滚动视图的顶层来对您的滚动视图进行子类化,您可以实现您想要做的事情。



这是理论! / p>

但这是我会尝试的。
查找此方法及其行为和意图

  +(BOOL)needsDisplayForKey:(NSString *)key 

您可以阅读的另一件事是开始示例期间。但这也意味着你应该自己重绘你的uiscrollview!


Sorry for a big question, real question are bold in bottom, now some explanation.

I'm use CoreAnimation in my project for animate some objects moving like this.

CABasicAnimation *moveAnimation;        
moveAnimation=[CABasicAnimation animationWithKeyPath:@"position"];      
moveAnimation.duration = 2.0;       
moveAnimation.repeatCount=1;        
moveAnimation.autoreverses=NO;  
moveAnimation.delegate = self;
moveAnimation.fromValue = [NSValue valueWithCGPoint:[crawler.layer position]];
moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.removedOnCompletion = NO;
CGPoint p;
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
[crawler.layer addAnimation:moveAnimation forKey:@"moveAnimation"];

And now I need to animate scrolling of some UIScrollView. I can't use setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method, because it can't customise animation duration.

I can make it like this

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x + 200, scrollView.contentOffset.y);
[UIView commitAnimations];

but it's not CoreAnimation, and using 2 different kinds of animation techniques in one app is not a good practice, I think.

Is there a way to use a CoreAnimation to animate scrolling of UIScrollView content to custom point with custom animation duration? P.S. Sorry for the bad English.

解决方案

You should read about animatable properties, Programming iOS 5

Indeed, when you try to animate layers, Apple gives a set of animatable properties, but you're not limited to this set. You can animate whatever you want.

In all cases, you shouldn't use kCAFillModeForwards because it will ask the device to redraw forever the last presentation layer that was used for the animation, instead of using a cached representation of the layer.

So the way you could achieve such a thing would be by defining a specific layer of yours where the contentOffset property is defined as animatable. And in order to work, you should start by verifying what kind of top layer class is used by a scrollview, so that, by subclassing it and setting the wanted property (contentOffset) animatable, and subclassing your scrollview by setting this layer as the top layer of the scrollview, you could possibly achieve what you're trying to do.

This is theory!

But this is what I would try. Look for this method and its behavior and intents

+ (BOOL)needsDisplayForKey:(NSString *)key

Another thing you could read is this A start example. But this also means you should redraw your uiscrollview by yourself!

这篇关于使用CoreAnimation动画滚动UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆