動畫滾動的UIScrollView與CoreAnimation

[英]Animate scrolling of UIScrollView with CoreAnimation


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.

我在我的項目中使用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"];

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.

現在我需要對一些UIScrollView進行動畫滾動。我不能使用setContentOffset:(CGPoint)contentOffset animated:(BOOL) animation方法,因為它不能定制動畫持續時間。

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.

但它不是CoreAnimation,我認為在一個應用程序中使用兩種不同的動畫技術不是一個好的實踐。

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.

是否有一種方法可以使用CoreAnimation將UIScrollView內容的滾動動畫到自定義動畫持續時間的自定義點?附注:對不起,我的英語不好。

4 个解决方案

#1


5  

You should read about animatable properties, Programming iOS 5

您應該閱讀關於可動畫屬性的文章,編寫ios5

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.

在所有情況下,都不應該使用kcafillmodeforward,因為它會要求設備永遠重新繪制用於動畫的最后一個表示層,而不是使用該層的緩存表示。

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.

因此,您可以通過定義您的特定層來實現這樣的目標,在這個層中contentOffset屬性被定義為animatable。為了工作,你應該開始通過驗證什么樣的頂層類使用一個滾動視圖,因此,通過子類化並設置想要的屬性(內容偏移)可以做成動畫,和子類化滾動視圖通過設置該層的頂層滾動視圖,你可以達到你想要做什么。

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!

另一件你可以讀的事情是這是一個開始的例子。但這也意味着你應該自己重新繪制uiscrollview !

#2


27  

You could use:

您可以使用:

[UIView animateWithDuration:duration animations:^ {
    [scrollView setContentOffset:destination animated:NO];
}];

You can set the duration of the animation this way.

您可以這樣設置動畫的持續時間。

#3


7  

[UIScrollViewNameHere setContentOffset:CGPointMake(x value of where you want to move to, y value of where you want to move to) animated:YES];

for example:

例如:

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(4, 2, 2341, 412)];
[scroll setContentOffset:CGPointMake(320, 0) animated:YES];

#4


4  

I don't think so that you can animate the point with CoreAnimation.

我不認為你可以用CoreAnimation做動畫。

But you can try setContentOffset:animated: thats the method you are searching for?

但是你可以試試setContentOffset:animated:那是你要尋找的方法嗎?

You can transform only following properties of UIView

你只能轉換UIView的下列屬性

  • frame
  • 框架
  • bounds
  • 界限
  • center
  • 中心
  • transform
  • 變換
  • alpha
  • α
  • backgroundColor
  • 寫成backgroundColor
  • contentStretch
  • contentStretch

more Information of the CoreAnimation about animation checkout the Apple Documentation

有關動畫的更多信息請查看蘋果文檔


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/02/23/7300e901ac6c3379ea1da055d49df681.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com