【问题标题】:UIActivityIndicatorView not animatingUIActivityIndi​​catorView 没有动画
【发布时间】:2011-11-17 20:57:43
【问题描述】:

我不断让 UIActivityIndi​​catorView 进入视图,但没有动画。

我没有做大量的处理,我只是将我的类作为子视图添加到 UITableViewController 的视图中,并且什么都不做。

这是我用来布局 UIActivityIndi​​catorView 的类:

#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


【解决方案1】:

鉴于您的实现,您无需在didMoveToSuperviewremoveFromSuperView 中开始/停止动画。创建时将activityIndictorView 设置为动画。它只有在将UIXLoaderView 添加到它的超级视图时才可见,当您摆脱UIXLoaderView 时它会消失。

【讨论】:

  • 我已经尝试在几个地方启动它。在构造函数中,通过创建方法 startAnimating 和 stopAnimating 就像在 UIActivityIndi​​catorView... 但在 UITableViewController 它只会在滚动 tableView 后动画。很奇怪。
【解决方案2】:

你有没有试过在调用 super 之前启动动画?

【讨论】:

  • 可以,但不起作用。我也尝试过从 UITableViewController 调用它,但得到了相同的结果。它出现并且似乎开始了动画,因为我可以看到仪表,但它似乎卡住了,好像线程卡在一个很长的过程中但事实并非如此,一旦我滚动 tableView,它就会继续旋转。但在触摸滚动 tableView 之前,似乎应用程序没有响应静态仪表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多