【发布时间】:2011-11-17 20:57:43
【问题描述】:
我不断让 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,动画将开始,但一旦控件加载它就不会动画......而且我无法弄清楚这里发生了什么。有什么线索吗?
【问题讨论】:
-
代码似乎正确;唯一奇怪的是,您将自定义视图添加为表格视图控制器的子视图,而此视图是 UITableView。现在表格视图是一个非常特殊的单元格,因此在其上添加自定义视图可能会产生一些奇怪的副作用,就像这样;您可以尝试使用带有简单 UIView 的基本视图控制器作为超级视图,然后在此视图中添加两个子视图:表格和您的自定义视图吗?抱歉,我在 iPad 上无法查看,这只是一个想法,不是答案!
-
好吧,我确实尝试了不同的方法;我没有将视图添加为 UITableViewController 的子视图,而是将其添加到应用程序 UIWindow 并且它起作用了。不是我希望它工作的那样,因为我不想每次只想显示活动指示器时都调用 App 委托,但对于测试来说很好。我认为问题与视图动画有关。
标签: iphone ios xcode xcode4 ios5