13

It seems to me that the first time I run an animation (animating the frame of a UIView, or the position of a CALayer, etc) it is quite choppy, and subsequent animations are smooth.

What would be causing this, and is there any way to pre-cache the animation?

Note: this question is quite similar to this one: UIImageView animations lag at first run, but UIImages are not being used in my animations.

Community
  • 1
  • 1
ryleigh
  • 218
  • 2
  • 11
  • You've mentioned that this is a duplicate of [UIImageView animations lag at first run](http://stackoverflow.com/questions/3382478/uiimageview-animations-lag-at-first-run). – Moshe Nov 08 '11 at 04:28
  • That's true, I did. I guess it's not as much of a duplicate as I thought, because I'm not dealing with UIImages in my animations. Edited that part of the question. – ryleigh Nov 08 '11 at 04:48
  • @ryleigh - Fair enough. It's a good question though, so have an upvote. – Moshe Nov 08 '11 at 04:58

3 Answers3

2

If you have a TextField, which I assume is what receives your user input. Use the UITextFieldDelegate methods Did and not Should

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//Do textfield animations and other view animations here

}

Don't do your animations in;

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//Don't do textfield animations and other view animations here
//This is where the system does its own animations; raising the keyboard, etc
}

Majid
  • 383
  • 1
  • 3
  • 8
  • What is the technical reason for your suggestion? Why do animations not run smoothly in "should"? – Ed Chin Feb 21 '13 at 16:49
0

Assuming you are using [UIImage imageNamed: @"herp.png"] the image will be cached. If the image is drawn a lot it will be unpacked into memory, else it will do a lazy load of the image and cause a stutter.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Nico
  • 3,826
  • 1
  • 21
  • 31
  • The animations I'm concerned with aren't using UIImages, though that's good to know. To be specific, in one case I'm removing a UITextView from its parent, adding it to another view, and using `[UIView animate...]` to animate its frame. The first time it runs it stutters, and after it's back on its original view and the animation is triggered again, it's smooth. – ryleigh Nov 08 '11 at 04:32
  • This includes the rendering of any item. If the text is never edited it will only need to be rendered once. This may still be causing your issue. However, after reading your other posts it's will more than likely have something to do with your keyboard popping up. – Nico Nov 08 '11 at 14:35
0

Run it in viewDidAppear, instead of viewWillAppear. Caching should occur after the image did appear, and it should look the same to the user.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • Well, the animations I'm dealing with are in response to user input, so I can't play them right when the view appears. I used `viewWillAppear` because I was hoping there was some way to play them before the user could see them (which does sounds silly). – ryleigh Nov 08 '11 at 04:46
  • Odd. Are you using the [UIView beginAnimations] methods or the block-based [UIView animateWithDuration: animations: ...] methods? What properties are you animating? Anything besides frame? How many of what kind of object? – Tim Nov 08 '11 at 05:40
  • Well in one case I'm removing a UITextView from its parent, adding it to another view, and using [UIView animateWithDurationsEtc...] to animate its frame. Though at the same time the animation plays, the keyboard is summoned, so I'm starting to suspect that the creation of the keyboard is what causes the animation to stutter in that case. Does that make sense, that the keyboard is now cached somewhere, so that it doesn't have to be re-created the next time it's needed? If so, is there any way to pre-initialize it? – ryleigh Nov 08 '11 at 05:58
  • But in another case, I'm animating a button, which involves: implicit animations of the `position`, `colors`, and `borderColor` of a CAGradientLayer, implicit animation of the `backgroundColor` of a CAShapeLayer, and a CABasicAnimation of the `shadowPath` of the CAShapeLayer. The stutter isn't so obvious or cut-and-dry in this situation - but it definitely seems that when the animation is played (after having not been played for awhile) it stutters, and if it is played repeatedly, it is smooth. Like it's a wheel, and it's creating a nice groove to run smoothly in, to force a metaphor. – ryleigh Nov 08 '11 at 05:59