我一直在查看UIActivityIndicatorView而不是动画。UIActivityIndicatorView不动画
我没有做大量的处理我只是将我的类作为子视图添加到UITableViewController的视图中,除此之外别无其他。
下面是我用它来布置我的UIActivityIndicatorView类:
#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但是一旦控件加载后它就不会生成动画了......我无法理解这里发生的事情。任何线索?
该代码似乎正确;唯一奇怪的是,您将自定义视图添加为表视图控制器的子视图,并且此视图是UITableView。现在表格视图是相当特殊的单元格,因此在其上添加自定义视图可能会产生一些奇怪的副作用,例如:你可以尝试使用一个简单的UIView作为超级视图的基本视图控制器,然后在这个视图内添加两个孩子:表和你的自定义视图?对不起,我在我的iPad上,我无法检查,这只是一个想法,不是一个答案! – viggio24
好吧,我尝试了一种不同的方法;而不是添加视图作为UITableViewController的子视图,我将它添加到应用程序UIWindow中并且工作正常。不是我希望它能够工作,因为我不想每次只想显示活动指示器时都调用应用程序委托,但是对于测试来说可以。我认为这个问题与观看动画有关。 – shadowfax