如何将动画设置为UIImageView?

tri*_*eni 5 iphone objective-c uiimageview ios

如何给UIImageView提供动画.我有以下动画代码.但不适用于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];
Run Code Online (Sandbox Code Playgroud)

有人有想法让我知道..谢谢

Kju*_*uly 6

UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage  cache : YES];
Run Code Online (Sandbox Code Playgroud)

[在代码之前松散?也许是类型错误.

我想你可能会喜欢这个,基于块的动画更好;):

[UIView transitionFromView:self.view
                    toView:MyImage
                  duration:1.0f
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:nil];
Run Code Online (Sandbox Code Playgroud)

这里的代码片段效果很好:

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];
Run Code Online (Sandbox Code Playgroud)

编辑:

您需要创建一个新视图并添加MyImage到该视图.新版本如下:

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];
Run Code Online (Sandbox Code Playgroud)