使用CoreAnimation动画滚动UIScrollView

Pad*_*van 16 iphone core-animation uiscrollview ios

对不起,一个大问题,真正的问题在底部是大胆的,现在有些解释.

我在我的项目中使用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"];
Run Code Online (Sandbox Code Playgroud)

现在我需要动画滚动一些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];
Run Code Online (Sandbox Code Playgroud)

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

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

sil*_*pop 26

你可以使用:

[UIView animateWithDuration:duration animations:^ {
    [scrollView setContentOffset:destination animated:NO];
}];
Run Code Online (Sandbox Code Playgroud)

您可以通过这种方式设置动画的持续时间.

  • "块动画"只是核心动画的包装,而不是一种不同的技术.你可以自由地混合他们. (3认同)

Sam*_*ten 7

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

例如:

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


Vas*_*ior 5

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

实际上,当您尝试为图层设置动画时,Apple会提供一组可动画的属性,但您不限于此设置.你可以动画任何你想要的动画.

在所有情况下,您都不应该使用,kCAFillModeForwards因为它会要求设备永久地重绘用于动画的最后一个表示层,而不是使用该层的缓存表示.

因此,您可以通过定义contentOffset属性的特定层来定义可动画的方式.并且为了工作,您应该首先验证scrollview使用哪种顶层类,以便通过子类化并设置所需的属性(contentOffset)动画,并通过将此图层设置为顶层来子类化您的scrollview在scrollview中,你可以实现你想要做的事情.

这是理论!

但这就是我想要的.查找此方法及其行为和意图

+ (BOOL)needsDisplayForKey:(NSString *)key
Run Code Online (Sandbox Code Playgroud)

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