我一直在查看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];
}
}
这里最奇怪的事情是,如果我点击并移动tableView,动画就会启动,但是一旦控件被加载它就不会动画了......我无法理解这里发生的事情。有线索吗?
答案 0 :(得分:1)
根据您的实施情况,您无需在didMoveToSuperview
和removeFromSuperView
中启动/停止动画。在创建时将activityIndictorView
设置为动画。它只有在UIXLoaderView
被添加到其超级视图时才会显示,当你摆脱UIXLoaderView
时它就会消失。
答案 1 :(得分:0)
您是否尝试过调用super之前启动动画?