17

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.

4 Answers 4

28

You could use:

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

You can set the duration of the animation this way.

3
  • Yes, I can,but this is block animation, not CoreAnimation, isn't it? I need a way to use CoreAnimation for this because already use it in project and don't want to use 2 different techniques.
    – Padavan
    Commented Feb 27, 2012 at 12:35
  • Wonderfully simple answer to the duration part of the question.
    – PKCLsoft
    Commented Apr 19, 2013 at 13:34
  • 3
    "block animation" is just a wrapper around Core Animation, not a different technology. you can mix 'em freely.
    – skozin
    Commented Apr 23, 2013 at 1:00
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];
1
  • I can't use setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method, because it can't customise animation duration. i write it in question, read it closely.
    – Padavan
    Commented Mar 1, 2012 at 15:27
5
+50

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!

1
  • Thank's a lot. This is what I need to now.
    – Padavan
    Commented Mar 5, 2012 at 8:58
4

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

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

You can transform only following properties of UIView

  • frame
  • bounds
  • center
  • transform
  • alpha
  • backgroundColor
  • contentStretch

more Information of the CoreAnimation about animation checkout the Apple Documentation

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.