如何为UIImageView设置动画?

5
如何给UIImageView添加动画效果。 我有以下的动画代码,但是对于UIImageView没有效果。
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];

有没有人有想法,让我知道一下..谢谢。
1个回答

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

在代码前松了一个 [ 吗?也许是打错了。

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

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

以下是有效的代码片段:

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

编辑:

您需要创建一个新视图并将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];

它能工作。但现在我得到了视图背景颜色为黑色。我放置了self.view.backgroundColor = [UIColor whiteColor]; 但视图颜色仍然是黑色。我应该写什么? - triveni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接