Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set animation to UIImageView?

How to give animation to UIImageView. I have a following code for animation .but not working for imageview

MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:@"image.png"];
MyImage.userInteractionEnabled = YES;
[self.view addSubview:MyImage];

[UIView beginAnimations : nil context : nil];
[UIView setAnimationCurve : UIViewAnimationCurveLinear];
[UIView setAnimationDuration : 1.00];

UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage  cache : YES];
[UIView commitAnimations];

Anybody have idea let me know ..Thanks

like image 968
triveni Avatar asked Dec 08 '11 10:12

triveni


People also ask

How do I add actions to UIImageView?

Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions. Then go to the view controller, drag the same object to set the IBAction.

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.


1 Answers

UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage  cache : YES];

Loose [ before the code? Maybe it's the type mistake.

And I think you may like this one, block-based animation is better ;) :

[UIView transitionFromView:self.view
                    toView:MyImage
                  duration:1.0f
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:nil];

And here's code snippet that works well:

MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:@"NTCHomeMainPic.png"];
MyImage.userInteractionEnabled = YES;
// [self.view addSubview:MyImage]; // You don't need to add it as subview, but never mind

[UIView transitionFromView:self.view
                    toView:MyImage
                  duration:1.0f
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:nil];

Edit:

You need to create a new view and add the MyImage to it. New version below:

UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[newView setBackgroundColor:[UIColor whiteColor]];
UIImageView * MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:@"NTCHomeMainPic.png"];
MyImage.userInteractionEnabled = YES;
[newView addSubview:MyImage];

[UIView transitionFromView:self.view
                    toView:newView
                  duration:1.0f
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:nil];
like image 51
Kjuly Avatar answered Sep 19 '22 04:09

Kjuly