0

我正在使用 iOS 食谱 PragProg 书中的食谱,这是我正在使用的代码的原始部分:

- (void)addActivityIndicatorView {

    activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;                       
    CGRect aiFrame = activityIndicatorView.frame;
    CGFloat originX = (self.view.bounds.size.width - aiFrame.size.width) / 2;
    CGFloat originY = (self.view.bounds.size.height - aiFrame.size.height) / 2;
    aiFrame.origin.x = floorl(originX);
    aiFrame.origin.y = floorl(originY);
    activityIndicatorView.frame = aiFrame;
    [self.view addSubview:activityIndicatorView];
}

我意识到我想要这个activityindicator代码用于我的其他控件上的多个activityIndi​​catorViews,这些控件通过Web加载数据,所以这是我制作可重用代码的实现,它没有像上面的代码对一个视图控制器那样在控件上显示activityIndi​​cator视图它在里面。是的,我确实为使用它的每个控件单步执行此代码,我调用 [activityIndi​​cator startAnimating]; 对于我传递给这个函数的activityIndi​​cator。猜猜我要问的是我应该在代码的其他地方查看哪里?

- (void)addActivityIndicatorView:(UIActivityIndicatorView *)acitivityIndicator toView:(UIView *)view {

    acitivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    acitivityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;                       
    CGRect aiFrame = acitivityIndicator.frame;
    CGFloat originX = (view.bounds.size.width - aiFrame.size.width) / 2;
    CGFloat originY = (view.bounds.size.height - aiFrame.size.height) / 2;
    aiFrame.origin.x = floorl(originX);
    aiFrame.origin.y = floorl(originY);
    acitivityIndicator.frame = aiFrame;
    [view addSubview:acitivityIndicator];
}
4

1 回答 1

2

像这样写你的方法:

- (void)addActivityIndicatorView:(UIActivityIndicatorView **)activityIndicator toView:(UIView *)view {

    UIActivityIndicatorView * activeInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activeInd.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;                       
    CGRect aiFrame = activeInd.frame;
    CGFloat originX = (view.bounds.size.width - aiFrame.size.width) / 2;
    CGFloat originY = (view.bounds.size.height - aiFrame.size.height) / 2;
    aiFrame.origin.x = floorl(originX);
    aiFrame.origin.y = floorl(originY);
    activeInd.frame = aiFrame;

    *activityIndicator = activeInd;
    [view addSubview:*activityIndicator];
}

当您想使用上述方法时,请将对 a 的引用传递UIActivityIndicatorView *给该方法,例如:

-(void) viewDidLoad
{
  [super viewDidLoad];

  UIActivityIndicatorView * ai;
  [self addActivityIndicatorView: &ai toView: self.view];

  activityIndicator = ai;
}

现在,每当您希望它开始动画时:

[activityIndicator startAnimating];
于 2012-06-17T04:44:41.750 回答