How to set animation to UIImageView?

2019-04-08 18:33发布

问题:

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

回答1:

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];