Objective-C UIViewImage animation

2019-02-11 12:55发布

Right now I'm able to do a simple animation using the code below. However, the below solution needs me to predefine the image names. How can I do something like NSArray.push("image1.png"); for example? Then assign the dynamic array as the animationImages?

Thank you, Tee

UIImageView* animationView = [[UIImageView alloc] initWithFrame:self.view.frame];

animationView.animationImages = [NSArray arrayWithObjects:  
[UIImage imageNamed:@"transition 1.png"],
[UIImage imageNamed:@"transition 2.png"],
[UIImage imageNamed:@"transition 3.png"],
[UIImage imageNamed:@"transition 4.png"],
[UIImage imageNamed:@"transition 5.png"], nil];

3条回答
看我几分像从前
2楼-- · 2019-02-11 13:20

You can use NSMutableArray, and add objects to it using addObject:. For example:

NSMutableArray *a = [NSMutableArray array];
[a addObject:[UIImage imageNamed:@"transition 1.png"]];
[a addObject:[UIImage imageNamed:@"transition 1.png"]];

animationView.animationImages = a;
查看更多
相关推荐>>
3楼-- · 2019-02-11 13:27
-(void)animate
{
name.animationImages = [NSArray arrayWithObjects:    
                                [UIImage imageNamed:@"1.png"],
                                [UIImage imageNamed:@"2.png"],
                                [UIImage imageNamed:@"3.png"],
                                 nil];
//duration of whole animation
name.animationDuration = 3;
// repeat the annimation forever
name.animationRepeatCount = 0;
// start animating
[name startAnimating];
// add the animation view to the main window 
[self.view addSubview:name];

}

That's the way I animate images. Just call animate wherever and it'll do its stuff

查看更多
相关推荐>>
4楼-- · 2019-02-11 13:31

@pgb, thank you your snippet works. I was using [[NSMutableArray alloc] init] instead of [NSMutableArray array] and that seems to be the issue.

Thanks, Tee

查看更多
登录 后发表回答