如何使活动指标立即开始制作动画

时间:2014-07-03 06:46:51

标签: ios objective-c xcode

在我的代码中,我试图让活动指示器开始动画RIGTH,但出于某种原因,当我点击我的按钮时,它首先进入下一页。当我回到上一页时,它会显示指示器旋转。这真让我抓狂。以下是我在viewdidLoad中的代码,以便在您第一次访问该页面时隐藏它:

- (void)viewDidLoad
{
[super viewDidLoad];
searchedItem.delegate = self;
activityInd.hidden = YES;

}

这是我的其他代码,用于检查我的按钮是否处于选定模式,以便我可以启动动画:

-(IBAction)ClickBtn:(UIButton *)sender
{
sender.selected  = ! sender.selected;

if (sender.selected)
{
    activityInd.hidden = NO;
    [activityInd startAnimating];
}
else
{
    NSLog(@" Selected");
}
}

我真的不确定为什么它等着我先离开页面然后回来让它开始旋转。

感谢所有帮助,提前感谢:)

4 个答案:

答案 0 :(得分:0)

我通常将指标控件类作为单例,并使指标的superview app委托窗口。每当您显示指示器时,指示器将显示在关键窗口上。

@interface Indicator : UIActivityIndicatorView
+ (void)show;
+ (void)hide;
@end

@implementation Indicator

+ (Indicator *)shared
{
    static Indicator *indicator;
    if(indicator == nil)
    {
        indicator = [[Indicator alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [indicator setCenter:[[UIApplication sharedApplication] keyWindow].center];
    }
    return indicator;
}

+ (void)show
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:[Indicator shared]];
    [[Indicator shared] startAnimating];
}

+ (void)hide
{
    [[Indicator shared] removeFromSuperview];
    [[Indicator shared] stopAnimating];
}

@end

喜欢这个。它只是我刚编码的样本。无论你想在哪里显示指标,[指标显示]都会让事情发生。

答案 1 :(得分:0)

在你的IBaction方法中添加,

    activityInd.hidden = NO;
    [activityInd startAnimating];

[self performSelector:@selector(goToNextPage) withObject:nil afterDelay:2.0];

然后,

在下面添加此方法。

- (void) goToNextPage
{
    //call your second page here

}

答案 2 :(得分:0)

- (void)viewWillAppear:(BOOL)animated
{
        searchedItem.delegate = self;
        activityInd.hidden = YES;


    [super viewWillAppear:animated];
}

答案 3 :(得分:-1)

    {
            spinner = [[UIActivityIndicatorView alloc]  initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
                spinner.frame = CGRectMake(40, 90, 24, 24);
                [self.view addSubview:spinner];

                [spinner setHidden:YES];

            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(10.0, 10.0, 30.0, 49.0);
            btn.backgroundColor = [UIColor redColor];
            [self.view addSubview:btn];
            [btn addTarget:self action:@selector(ClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }

-(IBAction)ClickBtn:(UIButton *)sender
{
    sender.selected  = ! sender.selected;

    if (sender.selected)
    {
        spinner.hidden = NO;
        [spinner startAnimating];
    }
    else
    {
        NSLog(@" Selected");
    }
}