iphone - moving a UIImageView - iphone

I'm having difficulty moving an image to another location after the first animation is finished.
the image animates at a point that I've specified, then stops (that works fine). I would then like to move the image to another location and repeat.
here's my code:
-(void) loadTap {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tap1.png"],
[UIImage imageNamed:#"tap2.png"],
[UIImage imageNamed:#"tap3.png"],
[UIImage imageNamed:#"tap4.png"],
nil];
tapImage.animationImages = imageArray;
tapImage.animationRepeatCount = 1;
[imageArray release];
tapImage.animationDuration = 1;
tapImage.animationRepeatCount = 20;
[tapImage startAnimating];
tapImage.center = CGPointMake(156, 110);
}
thanks for any help.

In order to move an image, you should enclose the code to move in an animation block:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];
You can also give a method to execute upon completion of the animation with UIView setAnimationDidStopSelector: method.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animateImages)];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];
//other stuff
-(void)animateImages{
[tapImage startAnimating];
}

Related

How can I implement image Flip Animation in iphone app

How can I implement image flip animation towards right. the code given below. plz help .
imageview.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"imag2.png"],
[UIImage imageNamed:#"imag3.png"],
[UIImage imageNamed:#"imag4.png"],
[UIImage imageNamed:#"imag5.png"],
[UIImage imageNamed:#"imag6.png"],
[UIImage imageNamed:#"imag7.png"], nil];
imageview.animationDuration=15.0;
imageview.animationRepeatCount=0;
[imageview startAnimating];
for vertical flip
[UIView animateWithDuration:1.0 animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
} completion:^(BOOL finished){
// code to be executed when flip is completed
}];
for horizontal flip
[UIView animateWithDuration:1.0 animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,0.0,1.0,0.0);
} completion:^(BOOL finished){
// code to be executed when flip is completed
}];
and don't forget to add QuartzCore framework to the project and importing
#import <QuartzCore/QuartzCore.h>
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:imageview cache:NO];
[UIView commitAnimations];
I think This code may help you.

UIView CATransform3DMakeRotation horizontally from left to right

I am trying to rotate my view horizontally from left to right. So I am using CATransform3DMakeRotation to do rotation.
Here is my code,
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
newView.layer.transform = CATransform3DMakeRotation(M_PI/2,0,1.0,0.0);
[UIView commitAnimations];
This code really works, But it rotates my view half only.. I want to make full rotation.
So i changed the value M_PI/2 into M_PI, like this
CATransform3DMakeRotation(M_PI,0,1.0,0.0);
But for this value, my view is not rotating at all. Please give me some suggestion.
UIImageView *imageView = [[[UIImageView alloc] init] autorelease]; // Don't forget to release this! I added autorelease.
imageView.image = [UIImage imageNamed:#"photo1.png"];
imageView.frame = CGRectMake(0, 0, viewOutlet.frame.size.width, viewOutlet.frame.size.height);
[viewOutlet addSubview:imageView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
viewOutlet.layer.anchorPoint = CGPointMake(0.0f, 0.5f);
viewOutlet.layer.transform =CATransform3DMakeRotation(M_PI,0,1.0,0.0);
[UIView commitAnimations];
** viewOutlet is Your view outlet so don't forget to IBOutlet it.

Trying to standardise UIView animations in my app, and it's not working

I have the same animation code applied to various views in my object, so i thought I would standardise it with it's own class. Here is the code i have:
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface AHAnimations : NSObject
+(void)popInView:(UIView *)view;
#end
.m
#import "AHAnimations.h"
#implementation AHAnimations
+(void)popInView:(UIView *)view {
view.alpha = 0;
view.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
view.alpha = 1;
view.transform = CGAffineTransformMakeScale(1, 1);
}completion:nil];
}
#end
But instead of animating, the view just appears.
Any ideas why this isn't working?
EDIT: The original code is exactly the same. I can paste it, but other than being a different method name, i'd just be copying and pasting it from above. And now, it is simply [AHAnimations popInView:view];
UIView *myview=[[UIView alloc] init];
[myview setFrame:CGRectMake( 50.0f, 50.0f, 280.0f, 400.0f)];
[UIView commitAnimations];
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.7];
[myview setFrame:CGRectMake( 5.0f, 70.0f, 310.0f, 350.0f)];
// CALL This line to stop animation
[myview.layer removeAllAnimations];
[UIView commitAnimations];
+[UIView animateWithDuration ... not animating all properties, tho alpha should work...
These code won't work, you can use CAAnimationGroup or CABasicAnimation's property: fromValue, byValue, toValue.
Also, you could implement like this which is easier to understand,
//go
- (void)stepOne{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(stepTwo)];
view.alpha = 0;
view.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];
}
//back
- (void)stepTwo{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.1];
view.alpha = 1;
view.transform = CGAffineTransformMakeScale(1, 1);
[UIView commitAnimations];
}
There are also a lot of original transition animation, you can check them out.

How to animate a UITextView that is a subview?

I am trying to animate a UITextView (myTextView) that is a subview inside a UIScrollView (scrollView).
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:myTextView cache:YES];
[scrollView addSubview:myTextView];
[UIView commitAnimations];
myTextView appears without animation!
What kind of animation do you want to do? If you want to fade in, for example, you have to do
[scrollView addSubview:myTextView];
myTextView.alpha = 0.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:myTextView cache:YES];
myTextView.alpha = 1.0;
[UIView commitAnimations];
You can change several properties (e.g.: frame, transform) to make animations.
http://www.4shared.com/dir/-PGqZEXR/zoominzoomout.html just add this file with your project and call it through this functions
here im2 is UIVIEW and i paste image on it then passing this through view didload like this
- (void)viewDidLoad {
[self loadFlowersInView:im2];
[self.view addSubview:im2];
}
now attach this function like this
- (void) loadFlowersInView: (UIView *) backdrop {
NSString *create_button = nil; // Add the flowers to random points on the screen
for (int i = 0; i < 1; i++) {
MagPicAppDelegate *appdelegate = (MagPicAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *whichFlower = appdelegate.imageName;
UIImage *stemp = [UIImage imageNamed:#"approve.png"];
DragView *dragger = [[DragView alloc] initWithImage:stemp];
dragger.userInteractionEnabled = YES;
dragger.center = CGPointMake(160.0f, 140.0f);
[im2 addSubview:dragger];
[dragger release];
}
[self.view addSubview:im2];
[im2 release];
}

Spawning infinite UIImageViews without name collisions

iPad app. OS 4.2.
I have a button that, when pressed, calls this function that creates a UIImageView and animates it (and calls a second animation block). I created another button that calls the same function and passes different locations and different URL to a graphic.
I'm starting to get glitches—the first graphic spawned from the first button has the locations passed from the second button. This is probably due to the fact that I am not dynamically naming the UIImageView and getting collisions from that.
So how do I dynamically create and name any infinite number of UIImageViews? Especially since, to reference the UIImageView in two functions, it needs to be declared outside of the functions.
-(void)makeSoundEffectWordAppear:(NSString *)imageName:(int)startingX:(int)startingY:(int)endingX:(int)endingY{
myImage = [UIImage imageNamed:imageName];
[testWord setImage:myImage];
testWord = [[[UIImageView alloc] initWithFrame:CGRectMake(startingX,startingY,myImage.size.width,myImage.size.height)] autorelease];
[self.view addSubview:testWord];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView beginAnimations:#"moveWord" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeWord:finished:context:)];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationBeginsFromCurrentState:YES];
testWord.transform = CGAffineTransformMakeScale(1, 1);
testWord.center = CGPointMake(endingX,endingY);
testWord.alpha=1;
[UIView commitAnimations];
}
- (void)fadeWord:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
[UIView beginAnimations:#"makeWordFade" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:5];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}
You have a single pointer to testWord rather than a pointer for each instance. You should be using the context property of animations to pass in the specific UIImageView you want to fade away:
-(void)makeSoundEffectWordAppear:(NSString *)imageName:(int)startingX:(int)startingY:(int)endingX:(int)endingY{
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *testWord = [[[UIImageView alloc] initWithImage:myImage] autorelease];
CGRect frame = [testWord frame];
frame.origin.x = startingX;
frame.origin.y = startingY;
[testWord setFrame:frame];
[self.view addSubview:testWord];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView beginAnimations:#"moveWord" context:testWord];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeWord:finished:context:)];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationBeginsFromCurrentState:YES];
testWord.transform = CGAffineTransformMakeScale(1, 1);
testWord.center = CGPointMake(endingX,endingY);
testWord.alpha=1;
[UIView commitAnimations];
}
- (void)fadeWord:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
UIImageView *testWord = (UIImageView *)context;
[UIView beginAnimations:#"makeWordFade" context:context];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:5];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}