简体   繁体   English

逐帧动画UIImageView

[英]Animating UIImageView frame by frame

I have an idea about how to add animated UIImageView using frame by frame method BUT my question is about How to animate UIImageView ALREADY added on view controller storyboard as IBOutlet UIImageView . 我有一个关于如何使用逐帧方法添加动画UIImageView的想法,但我的问题是关于如何对UIImageView进行动画处理,该方法已经作为IBOutlet UIImageView添加到了视图控制器故事板上。

what will be changed at this peace of code ?! 在这种和平的代码中将发生什么变化?

NSArray *myImages = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"bookmark1.png"],
                         [UIImage imageNamed:@"bookmark2.png"],
                         [UIImage imageNamed:@"bookmark3.png"],
                         [UIImage imageNamed:@"bookmark4.png"],
                         [UIImage imageNamed:@"bookmark5.png"],nil];

    myAnimatedView = [UIImageView alloc];

   [myAnimatedView initWithFrame:CGRectMake(0, 0, 131, 125)];

    myAnimatedView.animationImages = myImages;

    myAnimatedView.animationDuration = 0.25;

    myAnimatedView.animationRepeatCount = 1;

    [myAnimatedView startAnimating];

    [self.navigationController.view addSubview:myAnimatedView];

I want to animate this image like that over my viewController 我想在我的viewController上为该图像设置动画

http://imageshack.us/a/img717/3051/bookmarkl.gif http://imageshack.us/a/img717/3051/bookmarkl.gif

It's even easier. 更容易了。 First, add in .h file: 首先,添加.h文件:

@property (nonatomic, retain) IBOutlet UIImageView *iV;

After, connect this outlet to the actual UIImageView on your storyboard. 之后,将此插座连接到情节提要上的实际UIImageView。 Change your code: 更改您的代码:

iV.animationImages = myImages;
iV.animationDuration = 0.25;
iV.animationRepeatCount = 1;
[iV startAnimating];

And that's all. 就这样。 Hope this helped 希望这有所帮助

ps And yes, don't forget iV = nil; ps是的,不要忘记iV = nil; in - (void)viewDidUnload method in-(void)viewDidUnload方法

UPD: added UPD:已添加

[self performSelector:@selector(animationDone) withObject:nil afterDelay:0.25];

After startAnimating call, and obviously added animationDone method: 在startAnimating调用之后,显然添加了animationDone方法:

- (void)animationDone {
    [iV stopAnimating];
    [iV setImage:[UIImage imageNamed:@"bookmark5.png"]];
}

Well it will be pretty much the same. 好吧,几乎一样。 You dont need to allocate your image view [[UIImageView alloc] init] nor do you need to add it to the viewController. 您不需要分配图像视图[[UIImageView alloc] init]也不需要将其添加到viewController。 The rest is the same. 其余部分相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM