0

I started working on sample application, where I have some objects(images) which are animated on a UIView. I would like to have a background image which also should animate along with these objects.Two animations should happen at the same time. The background image occupies all the screen space and on the top of this the other images should be animated. Could someone guide me to achieve this.

I tried the following things mentioned in the link below.

  for (int i = 0; i < IMAGE_COUNT; i++)
        [_imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"D%d.png", i]]];


    _animatedImages = [[UIImageView alloc] 
                      initWithFrame:CGRectMake(
                                               (SCREEN_WIDTH / 2) - (IMAGE_WIDTH / 2), 
                                               (SCREEN_HEIGHT / 2) - (IMAGE_HEIGHT / 2) + STATUS_BAR_HEIGHT,
                                               IMAGE_WIDTH, IMAGE_HEIGHT)];

    _animatedImages.animationImages = [NSArray arrayWithArray:_imageArray];

    _animatedImages.animationDuration = 1.0;

    _animatedImages.animationRepeatCount = -1;

The iPhone Game Background as Video or Animated Image?

But two animations are happening independently. I would like to animate the objects on the top of background image which also need to be animated.If this is not possible can I use layers or some other object to achieve this.Please suggest.

Community
  • 1
  • 1
Ram
  • 1,872
  • 5
  • 31
  • 54
  • http://stackoverflow.com/questions/8112698/how-to-do-animations-using-images-efficiently-in-ios/17129053#17129053 – MoDJ Jun 18 '13 at 15:34

1 Answers1

0

It seems like `UIImageView' provides no way to control the time the animation begins. There is such an API, but it's in lower level CoreAnimation framework (which UIImageView must use in its implementation...)

You could re-implement you own UIImageView animation.

Then, to set different animations with relative timing, see "How can I chain Core Animations for different Layers one after the other?" and How to group two CABasicAnimation animations and kick them off at the exact same time?.

As you don't need an offset between all your layers animations but want them to run at the same time, you should create a CAAnimationGroup, and put all of them with the same beginTime.

The CAMediaTiming protocol (implemented by CAAnimation) is pretty baldy documented - but this is the part you're interested in : timing your Core Animations - Check this post for some API clarification

Community
  • 1
  • 1
Vinzzz
  • 11,746
  • 5
  • 36
  • 42
  • Vinzz: I would like to implement something like a race, where the persons should be animated at the same time the background should also be animated.Could you let me know what I need to use here.Can I use layers here? – Ram Feb 11 '13 at 17:04
  • Whether you use UIView subclasses (as UIImageView), or directly CALayer, they both use CALayers to draw - so, YES, you can use layers... everywhere ! The idea is to use UIKit whenever possible (simpler API), and dive into CoreAnimation if you can't get the effect you're looking for. If all your animations are defined in UIKit API, you should group all your animations in a single `+UIView animateWithDuration:delay:options:animation:` . This method automatically creates a `CATransaction` that set animations on a same CoreAnimation commit : your animations should be synchronised... – Vinzzz Feb 11 '13 at 17:56