2011-11-17 66 views
0

我一直在查看UIActivityIndi​​catorView而不是动画。UIActivityIndi​​catorView不动画

我没有做大量的处理我只是将我的类作为子视图添加到UITableViewController的视图中,除此之外别无其他。

下面是我用它来布置我的UIActivityIndi​​catorView类:

#import "UIXLoaderView.h" 
#import <QuartzCore/QuartzCore.h> 


@interface UIXLoaderView() { 
@private 
    UIActivityIndicatorView *activityIndicatorView; 
} 
@end 

@implementation UIXLoaderView 

- (id)init { 
    self = [super init]; 
    if (self) { 
     // Measure Loading string 
     NSString *loadingText = NSLocalizedString(@"Loading...", @"Loading..."); 
     CGSize textSize = [loadingText sizeWithFont:[UIFont systemFontOfSize:14] forWidth:280 lineBreakMode:UILineBreakModeTailTruncation]; 

     // Create activity gauge 
     activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 

     // now set our frame 
     if (activityIndicatorView.frame.size.width > textSize.width) { 
      [self setFrame:CGRectMake(0, 0, activityIndicatorView.frame.size.width + 20, activityIndicatorView.frame.size.height + textSize.height + 30)]; 
     } else { 
      [self setFrame:CGRectMake(0, 0, textSize.width + 20, activityIndicatorView.frame.size.height + textSize.height + 30)]; 
     } 

     // Add the activity gauge into the view 
     [activityIndicatorView setFrame:CGRectMake(((self.frame.size.width - activityIndicatorView.frame.size.width)/2) , 10, activityIndicatorView.frame.size.width, activityIndicatorView.frame.size.width)]; 
     [self addSubview:activityIndicatorView]; 

     // Create and add the label 
     UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(((self.frame.size.width - textSize.width)/2), activityIndicatorView.frame.origin.y + activityIndicatorView.frame.size.height + 10, textSize.width, textSize.height)]; 
     [loadingLabel setText:loadingText]; 
     [loadingLabel setFont:[UIFont systemFontOfSize:14]]; 
     [loadingLabel setLineBreakMode:UILineBreakModeTailTruncation]; 
     [loadingLabel setBackgroundColor:[UIColor clearColor]]; 
     [loadingLabel setTextColor:[UIColor whiteColor]]; 
     [self addSubview:loadingLabel]; 

     // rounded corners 
     [[self layer] setCornerRadius:9.0]; 
     //[[self layer] setBorderWidth:1.0]; 
     //[[self layer] setBorderColor:[[UIColor blackColor] CGColor]]; 

     // back color 
     [self setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f]]; 
    } 
    return self; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

- (void)removeFromSuperview { 
    // Stop animating 
    [activityIndicatorView stopAnimating]; 

    // Call super 
    [super removeFromSuperview]; 
} 

- (void)didMoveToSuperview { 
    // Move to center 
    [self setCenter:[[self superview] center]]; 

    // Call super 
    [super didMoveToSuperview]; 

    // Animate 
    [activityIndicatorView setHidden:NO]; 
    [activityIndicatorView startAnimating]; 
} 

@end 

头文件是:

#import <UIKit/UIKit.h> 

@interface UIXLoaderView : UIView 

@end 

里面一个UITableViewController我只是把它按以下方式:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // If no data start loading animation 
    if (!self->data) { 
     // Show the loading view 
     loaderView = [[UIXLoaderView alloc] init]; 
     [self.view addSubview:loaderView]; 
    } 

} 

这里最大的问题是动画将开始,如果我点击并移动tableV但是一旦控件加载后它就不会生成动画了......我无法理解这里发生的事情。任何线索?

+0

该代码似乎正确;唯一奇怪的是,您将自定义视图添加为表视图控制器的子视图,并且此视图是UITableView。现在表格视图是相当特殊的单元格,因此在其上添加自定义视图可能会产生一些奇怪的副作用,例如:你可以尝试使用一个简单的UIView作为超级视图的基本视图控制器,然后在这个视图内添加两个孩子:表和你的自定义视图?对不起,我在我的iPad上,我无法检查,这只是一个想法,不是一个答案! – viggio24

+0

好吧,我尝试了一种不同的方法;而不是添加视图作为UITableViewController的子视图,我将它添加到应用程序UIWindow中并且工作正常。不是我希望它能够工作,因为我不想每次只想显示活动指示器时都调用应用程序委托,但是对于测试来说可以。我认为这个问题与观看动画有关。 – shadowfax

回答

0

您是否曾尝试在调用super之前启动动画?

+0

是的,但不起作用。我也试着从UITableViewController调用它,但得到了相同的结果。它看起来似乎开始动画,因为我可以看到量表,但它似乎卡住了,就好像线程被困在一个漫长的过程中,但事实并非如此,一旦我滚动tableView,它就会旋转。但在触摸滚动tableView之前,它似乎应用程序没有响应静态标尺。 – shadowfax

1

根据您的实施情况,您不需要在didMoveToSuperviewremoveFromSuperView中开始/停止动画。设置activityIndictorView以创建动画。只有当UIXLoaderView被添加到超级视图时它才会显示,并且当您摆脱UIXLoaderView时它将消失。

+0

我试过在几个地方开始它。在构造函数中,通过在UIActivityIndi​​catorView中创建方法startAnimating和stopAnimating ...但在UITableViewController上它只会在滚动tableView之后生成动画。很奇怪。 – shadowfax

相关问题