UIActivityIndi​​catorView不旋转

时间:2012-04-03 10:46:57

标签: iphone objective-c uiactivityindicatorview

我在向UIActivityIndicatorView添加UIScrollView时遇到此问题;一切正常,除非滚动UIScrollView,否则它不会开始旋转。

任何人都可以帮我解决这个问题吗?

谢谢。

这里有一些代码:

    UIActivityIndicatorView *loaderActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loaderActivity.center = CGPointMake(87/2,y+56/2);
    loaderActivity.tag=tag;
    [mainScrollView addSubview:loaderActivity];
    [loaderActivity startAnimating];
    [loaderActivity release];

5 个答案:

答案 0 :(得分:6)

您需要在活动指示器上调用startAnimating以使其具有动画效果。或者在界面构建器中,您可以勾选“动画”复选框。

答案 1 :(得分:3)

在滚动视图中滚动之前它不是动画的事实是您在后台线程中调用startAnimating的症状。 UIKit调用应该在主线程中进行。

您可以通过添加以下代码来验证它是否在后台线程中发生:

if ([NSThread isMainThread]) {
    NSLog(@"Running on main thread.");
} else {
    NSLog(@"Running on background thread.");
}

您需要确保在问题中显示的所有代码都在主线程上运行。为此,您可以将代码更改为如下所示:

// this code would be wherever your existing code was
[self performSelectorOnMainThread:@selector(addActivityIndicatorToView:) withObject:mainScrollView waitUntilDone:YES];

// this would be a new method in the same class that your existing code is in
- (void) addActivityIndicatorToView:(UIView*) view {
    UIActivityIndicatorView *loaderActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loaderActivity.center = CGPointMake(87/2,y+56/2);
    loaderActivity.tag=tag;
    [view addSubview:loaderActivity];
    [loaderActivity startAnimating];
    [loaderActivity release];
}

答案 2 :(得分:2)

activityIndicator = [[UIActivityIndicatorView alloc] 
initWithFrame:CGRectMake(87/2,y+56/2);
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.tag=tag;
[mainScrollView addSubview:loaderActivity];
[activityIndicator startAnimating];
[activityIndicator release];

答案 3 :(得分:1)

我在startAnimating中拨打viewDidLoad电话并且无效的问题。我在viewWillAppear中移动了呼叫并且它有效!

答案 4 :(得分:0)

尝试在不同的线程中运行动画师。

NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(showProgress) object:nil];
[thread start];

//do whatever your want here

//call this when you want it stop animating
    [activityIndicator stopAnimating];
    [thread release];


- (void)showProgress{

    [activityIndicator startAnimating];

}