0

iOS Recipes 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];
}

Web経由でデータをロードする他のコントロールの複数のactivityIndi​​catorViewsにこのactivityIndi​​catorコードが必要であることに気付いたので、これは再利用可能なコードを作成するための実装であり、上記のコードが1つのビューコントローラーに対して行うようにコントロールに対するactivityIndi​​catorビューを表示していませんはい、それを使用するコントロールごとにこのコードにステップインし、[activityIndi​​catorstartAnimating]を呼び出します。この関数に渡す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 に答える