I am trying to rotate my object like shakes dice.
please suggest simplest way to implement it in my iphone application.
Any kind of sample code or documentation.
http://www.amazon.com/OpenGL-SuperBible-Comprehensive-Tutorial-Reference/dp/0321498828
If you want to do this without using OpenGL you can use a UIImageView and the set a series of animation images. By creating a series of images you can create a "rolling dice" effect. Here is an example of how to do this:
// create a UIImageView
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rollDiceAnimationImage1.png"]];
// position and size the UIImageView
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100);
// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more)
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rollDiceAnimationImage1.png"],
[UIImage imageNamed:#"rollDiceAnimationImage2.png"],
nil];
// set the new UIImageView to a property in your view controller
self.viewController.rollDiceImage = rollDiceImageMainTemp;
// release the UIImageView that you created with alloc and init to avoid memory leak
[rollDiceImageMainTemp release];
// set the animation images and duration, and repeat count on your UIImageView
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages];
[self.viewController.rollDiceImageMain setAnimationDuration:2.0];
[self.viewController.rollDiceImageMain.animationRepeatCount:3];
// start the animation
[self.viewController.rollDiceImageMain startAnimating];
// show the new UIImageView
[self.viewController.view addSubview:self.rollDiceImageMain];
You can modify the creation and setup including the size, position, number of images, the duration, repeat count, etc as needed. I've used this feature of UIImageView to create simple animation effects and it works great!
Please let me know if you try this and if it works for your needs. Also, post any follow up questions.
Bart
nice your example, but do you think it's possible to rotate / control the rotation with touch gesture ?
Related
Basically I have three dots that are supposed to get bigger and smaller in succession on the loading screen and I'm wondering what is the best way to do a simple animation like that - coding or otherwise?
For a simple animation you can use a UIImageView animation to animate a set of images (i.e. use the images as frames in the animation).
UIImageView* dotsImageView= [[UIImageView alloc] initWithFrame:dotsFrame];
// load all the frames of your animation
dotsImageView.animationImages = #[[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"]];
// set how long it will take to go through all images
dotsImageView.animationDuration = 1.0;
// repeat the animation forever
dotsImageView.animationRepeatCount = 0;
// start the animation
[dotsImageView startAnimating];
// add it to the view
[self.view addSubview:dotsImageView];
If you don't want to use preset images for the dots, you can chain together UIView animations using the completion block. Here's a tutorial on UIView animations:
http://www.raywenderlich.com/5478/uiview-animation-tutorial-practical-recipes
How to Implement this kind of animation in iOS objective c.
Currently I have implemented "CABasicAnimation" for this but still not able to achieve this very smoothly .
any suggestion ??
Thank you
You'll need to use Core Image with a combination of different distortion filters.
You can then animate the intensity of the filters over time.
I don't think that with CABasic animation you can obtain that. Probably is a little "movie" made by combination of CIFilterd images (check CIFilter class in the doc). Another way around (but I'm writing as I'm thinking) is mask the image somehow with a CAShapeLayer and use CAAnimation on it.
As Leena pointed out this is a simple loop of images.
Create your frames outside Xcode and then animate them this way:
NSArray *frames = [NSArray arrayWithObjects:[UIImage imageNamed:#"firstFrame.png"],
[UIImage imageNamed:#"secondFrame.png"],
// add other frames
, nil];
UIImageView *animatedImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
animatedImage.animationImages = frames;
animatedImage.animationDuration = 3.0f; // seconds
animatedImage.animationRepeatCount = 0; // 0 = loops forever
[animatedImage startAnimating];
[self.view addSubview:animatedImage];
[animatedImage release];
i have a map (just an image) and i want that a user can click on some places on the map. I think i can do that with adding buttons as subviews. But i also want to animate them. So for example want to have a ring form around the link position. And this ring should animate like pulsing or so. How can i do that the best way?
greets Max
Not sure why nobody has tried to answer this but it's not too difficult.
First, create a UIImageView and configure it to animate your desired effect. The UIImageView documentation is very clear on what to do. Something like:
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frame1.png],
[UIImage imageNamed:#"frame2.png],
...,nil];
UIImageView *animatedImageView = [[UIImageView alloc] initWithFrame:myFrame];
animatedImageView.animatedImages = imageArray;
animatedImageView.userInteractionEnabled = YES;
[self.view addSubView:animatedImageView];
[animatedImageView startAnimating];
You then have to add the code to respond to touch events on the imageView.
Alternatively, you can create a UIButton subclass that displayed an animated UIImageView per above and then use the standard addTarget:action to respond to user actions.
I have an NSArray of images that I want to fade in/out between each other (much like how Flickr handles their home screen).
The problem I'm having is figuring out how to utilize the [UIView beginAnimations]; with the NSArray... any idea?
My code is below.
Thanks in advance!
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"show_robmachado.jpg"],
[UIImage imageNamed:#"show_losness.jpg"],
[UIImage imageNamed:#"show_blanchard.jpg"],
[UIImage imageNamed:#"show_tonino.jpg"],
nil];
homeAnimation.animationImages = myImages;
homeAnimation.animationRepeatCount = 0; // 0 = loops forever
homeAnimation.animationDuration = 7.0;
[homeAnimation startAnimating];
The array method is really for simple frame-based animations, not for transitions.
Instead I would have two UIImageViews at the same position, and set up a timer that would work through the array, using UIView animations to animate one image view fading to an alpha of 0 and the other to an alpha of 1. Then you'd get a kind of fading switch you are going for.
The .image property on a UIImageView might also be animatable, try in a timed loop changing .image on an image view in a UIView based animation block and see if you get a cross-fade.
I'm trying to make it so when my imageviews are spawned to the screen they scroll downwards but for some reason all my astroids do is spawn in place and don't move... here's my code.
astroidArray = [[NSMutableArray alloc] init];
UIImage *astroid = [UIImage imageNamed:#"astroidwhite.png"];
UIImageView *astroids = [[UIImageView alloc] initWithImage:astroid];
//set X and Y of the new imageView
astroids.center = CGPointMake(100 , 100 );
astroids.center = CGPointMake(astroids.center.x - astroidVelocity.x , astroids.center.y - astroidVelocity.y);
astroidVelocity = CGPointMake(0,5);
//add to array
[astroidArray addObject:astroids];
//add to the view, so it gets displayed.
[self.view addSubview: astroids];
I am not quite sure why they would move - if astroid velocity is fixed, this code will always put an asteroid at 100,100 offset by whatever the velocity value is...
is this code called repeatedly?
If so I'd only ever add the objects to the view once, and then just modify the astroids.center every time you want to move it.
Really though you should probably use CoreAnimation for this, or more likely some kind of 2D graphics engine using OpenGL like Cocos2D.