我一直让 UIActivityIndicatorView 进入 View ,但没有动画。
我没有进行大量处理,我只是将我的类作为 subview 添加到 UITableViewController 的 View 中,而不执行其他任何操作。
这是我用来布局 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,动画就会开始,但是一旦加载控件,它就不会动画......而且我无法弄清楚这里发生了什么。有什么线索吗?
最佳答案
根据您的实现,您不需要在 didMoveToSuperview
和 removeFromSuperView
中开始/停止动画。在创建时将 activityIndictorView
设置为动画。仅当将 UIXLoaderView
添加到其 super View 时它才可见,并且当您删除 UIXLoaderView
时它将消失。
关于iphone - UIActivityIndicatorView 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8174191/