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.
Related
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];
Hi everyone I'm french so scuse me for my english.My problem is the following: I have an image in the center of the screen called viewToRotate, then I have an image called flakeView that is created outside of the screen and then it is moving to the center, and every second a Timer does this(create a flakeView outside the screen and then move it to the center of the screen).
What I wanted to do was : if flakeView and viewToRotate collide reduce the alpha of viewToRotate to 0.5. But when flakeView appears on the screen the action of reducing the alpha is called without the collision of viewToRotate and flakeView, so they collide before they touches. I don't know why. How can I solve this please . Here is the code :
UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease];
// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);
// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;
// put the flake in our main view
[self.view addSubview:flakeView];
[UIView animateWithDuration:7
animations:^{
// set the postion where flake will move to
flakeView.center = viewToRotate.center;
}];
}
-(void)checkCollision{
if(CGRectIntersectsRect(flakeView.frame, viewToRotate.frame) == 1)
{
viewToRotate.alpha=0.5;
}
}
I would not suggest using a timer on viewDidLoad. I believe that you should set the timer at some other function. Even if you do not use viewDidLoad for anything else.
The other thing is the UIView animations. Even if you have a timer at every 60fps the attributes of the animated objects are not changed by the animation. So before you create the animation you set those properties once. And when you create the animation you set the same properties for the second time. And that's it. From now on while the animation is running if you do checks on the properties you will always get the second values.
To have a working code one option is to create the UIView animations (and perform the property checks) for every step at a certain time interval. Or use OpenGL.
Before getting too deep into this issue, please also make sure that your viewToRotate.frame is actually sized the way you expect it (see my comment).
Once you are certain about those attributes, you might want to check the collision on the current presentationLayer of your animated objects and not on their original state.
UIView.layer.presentationLayer
Returns a copy of
the layer containing all properties as
they were at the start of the current
transaction, with any active
animations applied.
So you might adapt your collision detection like this:
-(void)checkCollision
{
if(CGRectIntersectsRect(flakeView.layer.presentationLayer.frame, viewToRotate.layer.presentationLayer.frame) == 1)
{
viewToRotate.alpha=0.5;
}
}
Also consider using the CoreAnimation Layer method hitTest: instead of your handmade collision detection.
hitTest: Returns the farthest
descendant of the receiver in the
layer hierarchy (including itself)
that contains a specified point.
These are the following things you would want to check.
Dont init with image, use init with frame and then do
flakeView.image = ----; Because your image could be wide enough
initially , and hence colliding from the time you initialize.
Try alternatives to .center, like .frame.size.x , because I have had
problems with .center as well
You could probably NSLog the bounds of both ur imageviews before and
after reducing alpha
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 a UIButton in a viewcontroller's view, i wrote action method form that i am going to another view controller's view. Now i am trying to implement animation between to views.i tried with many types one of the type animation is shown below
[UIView beginAnimations:#"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.view addSubview:anotherView];
[UIView commitAnimations];
This animation is working well but i am trying to implement some "growing flower animation"(name may be different) means the other view pop up from the button co-ordinatesand occupy entire screen.my custombutton` co-ordinates are (500,500,20,20).can any one suggest me for implementing this animation.
Thank you.
This can be achieved by various methods.
You can use:-
1 )
Core animations
UIImage class's animations
Cocos 2D
UIAnimation Classes (by simple changing the frames)
..and many more
It actually depends on your requirements, and your comfortable level with these classes/technologies.
If you just want to display growing flower, then the simpler (but not the optimized, and best) solution is to create different images,and put them in array, and then animate it
A similar example is available at: -
Animating images
If you have step by step images of growing flower images, then you can simply implement this. Write a function to implement simple implementation,
NSMutableArray * images;
//allocates the images array.
images = [[NSMutableArray alloc] init];
//loops till the 18images are added into array.
for (int index = 1; index < 19; index++) {
//get the image name.
NSString * imageName = [NSString stringWithFormat:#"Animation%d",index];
//get the path for the image in resourse file.
NSString * path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
//allocate and own the image object for the particular image.
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
//adds it to array.
[images addObject:image];
//release after adding.
[image release];
//remove the useless pointer.
image = nil;
}
//give the pointer to the array holding the images.
imageView.animationImages = images;
//after using it, release it.
[images release];
//remove the useless pointer
images = nil;
//set animation duration
imageView.animationDuration = 3.00;
//start animate
[imageView startAnimating];
After when you want to stop the animation you can simply call
[imageView stopAnimation];
You have another choice of
imageView.repeatCount = 1;
imageView is an instance of uiimageView, allocated one.
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 ?