Uiimageview动画没有显示出来

时间:2016-07-27 06:08:36

标签: ios objective-c uiview uiimageview

我使用以下代码将图像置于视图中心

 UIImage* img1 = [UIImage imageNamed:@"1.gif"];
UIImage* img2 = [UIImage imageNamed:@"2.gif"];
UIImage* img3 = [UIImage imageNamed:@"3.gif"];
UIImage* img4 = [UIImage imageNamed:@"4.gif"];



UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, self.view.frame.size.height/4, 170, 170)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(65, 40, loadingView.bounds.size.width, loadingView.bounds.size.height)];
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES;
loadingView.layer.cornerRadius = 10.0;
loadingView.center = self.view.center;
imgView.backgroundColor = [UIColor clearColor];
imgView.center = loadingView.center;
imgView.animationImages = @[img1, img2, img3, img4];
imgView.animationDuration = 1.0*1.0;
[loadingView addSubview: imgView];
[self.view addSubview:loadingView];
[imgView startAnimating];
  1. 我最初定义了四个uiimages
  2. 我已经定义了loadingview,我将它集中在屏幕上
  3. 我定义了一个imageview,并将其集中到加载视图。
  4. 我已将imageview添加为loadingView的子视图,我已将加载视图添加到视图中。然后我将动画设为imageview
  5. 问题是我运行此代码时没有显示imaeview。我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您好,这是代码

UIImage* img1 = [UIImage imageNamed:@"1.gif"];
UIImage* img2 = [UIImage imageNamed:@"2.gif"];

UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, self.view.frame.size.height/4, 170, 170)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES;
loadingView.layer.cornerRadius = 10.0;
loadingView.center = self.view.center;
imgView.backgroundColor = [UIColor clearColor];
imgView.animationImages = @[img1, img2];
imgView.animationDuration = 1.0*1.0;
[loadingView addSubview: imgView];
[imgView startAnimating];
[self.view addSubview:loadingView];