Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-24-2011, 03:06 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 30
robotkid249 is on a distinguished road
Default Smooth Animation?

Hi,

I have this animation in a view based application which a UIImageView that spins 360 for an unlimited amount of time.. But every time the animation is about to end, it "skips."

How would I go about smoothing the animation so there is not skip every time the animation restarts?

Code:
 // starts the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    // sets the time before animation occurs
    // sets the length of the animation
    [UIView setAnimationDuration:10.0];
    [UIView setAnimationRepeatCount:1e100f]; //spin it forever times. 
    // 3.14 is pi. -3.14 would be counter clockwise
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14/2);
    //square is the UIImageView getting rotated
    square.transform = CGAffineTransformRotate(transform, 3.14/2);
    // ends animation
    [UIView commitAnimations];
robotkid249 is offline   Reply With Quote
Old 06-24-2011, 03:08 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

If the animation is set to run forever, how is it ever "about to end"? And FYI, you should look at the docs for setAnimationRepeatCount, you don't have to set it to some huge value to get the repeat forever ability.
smithdale87 is offline   Reply With Quote
Old 06-24-2011, 03:50 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 30
robotkid249 is on a distinguished road
Default

Yes, I'm new to coding, forgot to mention that I make mistakes. Because (if you look at the code) the image is set to rotate this much:
Code:
square.transform = CGAffineTransformRotate(transform, 3.14/2);
Which means that the animation stops and then repeats itself. So yes, the animation does "stop" and then keeps executing:
Code:
square.transform = CGAffineTransformRotate(transform, 3.14/2);
Thanks for your help, and how do I make it animate without skipping?
robotkid249 is offline   Reply With Quote
Old 06-24-2011, 04:12 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by robotkid249 View Post
Hi,

I have this animation in a view based application which a UIImageView that spins 360 for an unlimited amount of time.. But every time the animation is about to end, it "skips."

How would I go about smoothing the animation so there is not skip every time the animation restarts?

Code:
 // starts the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    // sets the time before animation occurs
    // sets the length of the animation
    [UIView setAnimationDuration:10.0];
    [UIView setAnimationRepeatCount:1e100f]; //spin it forever times. 
    // 3.14 is pi. -3.14 would be counter clockwise
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14/2);
    //square is the UIImageView getting rotated
    square.transform = CGAffineTransformRotate(transform, 3.14/2);
    // ends animation
    [UIView commitAnimations];

3.14 is not quite pi. It's a little less. There's a constant, M_PI, which is pi to many, many more digits. If you use that, you should get it to make a full 1/2 rotation for each cycle.

What exactly are you getting? Does it rotate 1/2 turn, then jump back to the beginning and start over?

If you specify full rotations using code like this:

square.transform = CGAffineTransformMakeRotation(M_PI * 2);


You should get a smooth full circle rotation.

Note that your code creates a transform of 3.14/2, and then applies another 3.14/3 rotation to that transform, which results in a total rotation of 3.14, or slightly less than half a full rotation (slightly less because 3.14 is less than pi, which is 3.1415926538...) There's no point in creating a trasnform with CGAffineTransformMakeRotation, and then transforming that transform with CGAffineTransformRotate. You're just making the CPU on the device work a little harder calculating those rotation transformations twice, by half the amount each time.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-24-2011, 09:11 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 30
robotkid249 is on a distinguished road
Default

Ok, I re-did the code to make it a little bit more streamlined. However when I do the (M_PI *2) to go 360˚ the animation does not even start.. Any suggestions?
Code:
 [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatCount:1e100f]; //spin it forever times. 
    square.transform = CGAffineTransformMakeRotation(M_PI * 2);
    [UIView commitAnimations];
robotkid249 is offline   Reply With Quote
Old 06-24-2011, 10:48 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by robotkid249 View Post
Ok, I re-did the code to make it a little bit more streamlined. However when I do the (M_PI *2) to go 360˚ the animation does not even start.. Any suggestions?
Code:
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatCount:1e100f]; //spin it forever times. 
    square.transform = CGAffineTransformMakeRotation(M_PI * 2);
    [UIView commitAnimations];
Hmm. I tested this using a CABasicAnimation on the layer. That works, because you set a rotation key value, rather than applying a rotation to the transform.
I guess you could link 180 degree rotations, where the completion routine (setAnimationDidStopSelector) increments the rotation by 180 degrees and invokes the animation again.

The CABasicAnimation code would look like this:


Code:
  CABasicAnimation* rotate =  [CABasicAnimation animationWithKeyPath: @"transform.rotation.z"];
  rotate.removedOnCompletion = FALSE;
  rotate.fillMode = kCAFillModeForwards;
  rotate.duration = 1.5;
  rotate.repeatCount = HUGE_VALF;
  rotate.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
  [rotate setToValue: [NSNumber numberWithFloat: M_PI * 2]];

  [square.layer addAnimation:  rotate forKey:  nil];

Doing half-turns using UIView animation might look something like this:

Add an iVar to your header:
Code:
CGFloat rotation;
Code:
//Call this method to start the endless spinning animation.
- (void) animateRotation;
{
  rotation += M_PI * 2.0 / 3.0;
  if (rotation > M_PI * 2)
  {
     //This probably isn't needed...
     rotation -= M_PI * 2;
  }
  [UIView beginAnimations:nil context:NULL];

  //Each 1/3 animation takes 1/3 second, so 1 RPM
  [UIView setAnimationDuration:1.0/3.0];  
  [UIView setAnimationCurve: UIViewAnimationCurveLinear];
  square.transform = CGAffineTransformMakeRotation(rotation);

  //The next 2 calls ask the system to call the
  //rotateAnimationDidStop:finished:context: method once the animation
  //is complete

  [UIView setAnimationDelegate: self];
  [UIView setAnimationDidStopSelector: 
    @selector(rotateAnimationDidStop:finished:context:)];
  [UIView commitAnimations];
}

- (void)rotateAnimationDidStop: (NSString *) animationID 
  finished: (NSNumber *) finished 
  context: (void *)context;
{
  [self animateRotation];  //start another 180 degree rotation
}

EDIT: There were several problems with the UIView animation code I posted.

1. The UIView animation uses a transform, and applies an increasing rotation to that transform. I posted code that increased the rotation by 180° (M_PI) per step. The system can't tell if a 180° change is supposed to be clockwise or counter-clockwise, so it gets confused and rotates back and forth instead of spinning.. I needed to change the rotation to 2/3 ∏ per step (a third of a rotation) so the system can tell which way to animate it. I changed the code above accordingly.

2. Since this code uses a did stop method to repeat the animation when it finishes, we don't want a repeat count.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 06-25-2011 at 10:45 AM.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, image, skip, smooth, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
8 members and 323 guests
chemistry, Dnnake, iOS.Lover, lendo, leostc, Leslie80, pbart, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0