iphone - UIActivityIndi​​catorView 没有动画

标签 iphone ios xcode xcode4 ios5

我一直让 UIActivityIndi​​catorView 进入 View ,但没有动画。

我没有进行大量处理,我只是将我的类作为 subview 添加到 UITableViewController 的 View 中,而不执行其他任何操作。

这是我用来布局 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,动画就会开始,但是一旦加载控件,它就不会动画......而且我无法弄清楚这里发生了什么。有什么线索吗?

最佳答案

根据您的实现,您不需要在 didMoveToSuperviewremoveFromSuperView 中开始/停止动画。在创建时将 activityIndi​​ctorView 设置为动画。仅当将 UIXLoaderView 添加到其 super View 时它才可见,并且当您删除 UIXLoaderView 时它将消失。

关于iphone - UIActivityIndi​​catorView 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8174191/

相关文章:

iphone - iOS UIWebView 问题

swift - 用于在 Xcode 中实现自定义 UI 设计的资源

iphone - 在 iOS 4.3 中,当您按下主页按钮或按下开/关按钮时,我如何区分后台模式?

ios - UIImageView 旋转但未正确重新定位

ios - 使用 UITextField 自定义自动 UIScrollView 滚动

ios - setlocale() 在 iOS 模拟器中不起作用?

ios - 将主 Storyboard中的按钮连接到 ViewController 中的@IBAction func operate()

ios - 如何在 objective c xcode 中传递开关的值

iphone - Xcode 内部 dealloc 保留计数 = 1

ios - Xcode 模拟器与我的设备。在我的设备上无法正常工作