Iphone UIActivityIndicatorView未设置动画
我一直在查看UIActivityIndicator视图,但没有设置动画 我没有进行广泛的处理,我只是将我的类作为子视图添加到UITableViewController的视图中,而不做其他事情 以下是我用来布局UIActivityIndicatorView的类:Iphone UIActivityIndicatorView未设置动画,iphone,ios,xcode,xcode4,ios5,Iphone,Ios,Xcode,Xcode4,Ios5,我一直在查看UIActivityIndicator视图,但没有设置动画 我没有进行广泛的处理,我只是将我的类作为子视图添加到UITableViewController的视图中,而不做其他事情 以下是我用来布局UIActivityIndicatorView的类: #import "UIXLoaderView.h" #import <QuartzCore/QuartzCore.h> @interface UIXLoaderView () { @private UIActivi
#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
这里最开始的事情是,如果我点击并移动tableView,动画将开始,但一旦加载控件,它将不会动画。。。我不知道这里发生了什么。有什么线索吗?在调用super之前是否尝试过启动动画?根据您的实现,您不需要在
didMoveToSuperview
和removeFromSuperView
中启动/停止动画。将活动指示器视图设置为在创建时设置动画。只有将UIXLoaderView
添加到它的superview时,它才可见,当你去掉UIXLoaderView
代码似乎正确时,它就会消失;唯一奇怪的是,您将自定义视图添加为表视图控制器的子视图,而此视图是UITableView。现在,表视图是一个非常特殊的单元,因此在其上添加自定义视图可能会产生一些奇怪的副作用,如下所示;您是否可以尝试将简单UIView作为superview使用基本视图控制器,然后在此视图中添加两个子视图:表和自定义视图?对不起,我在iPad上,无法检查,这只是一个想法,不是答案!我尝试了一种不同的方法;我没有将该视图添加为UITableViewController的子视图,而是将其添加到应用程序UIWindow中,结果成功了。我不想让它工作,因为我不想每次只想显示活动指示器时都调用应用程序代理,但可以进行测试。我认为这个问题与观看动画有关。我已经在几个地方尝试过启动它。在构造函数中,通过创建方法startAnimating和stopAnimating,如UIActivityIndicatorView中所示。。。但在UITableViewController上,它将仅在滚动tableView后才设置动画。很奇怪,是的,但不起作用。我还尝试从UITableViewController调用它,但得到了相同的结果。它似乎开始了动画,我可以看到仪表,但它似乎被卡住了,好像线程在一个漫长的过程中被卡住了,但事实并非如此,一旦我滚动tableView,它就会旋转。但在触摸滚动tableView之前,应用程序似乎没有使用静态仪表进行响应。
#import <UIKit/UIKit.h>
@interface UIXLoaderView : UIView
@end
- (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];
}
}