1

我正在尝试为 UIActivityIndi​​catorView 创建一个类类别 - 我想将它设置在屏幕中心。

于是我宣布:

@implementation UIActivityIndicatorView(Normalize)

-(UIActivityIndicatorView *) setAtScreenCenter{
    CGRect r = [UIScreen mainScreen].applicationFrame;
    CGRect wheelR = CGRectMake(r.size.width / 2 - 12, r.size.height / 2 - 12, 24, 24);
    self = [[UIActivityIndicatorView alloc] initWithFrame:wheelR];
    self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);
    return self;
}
@end

用法:[activityWheel setAtScreenCenter];

但是,我得到一个编译错误:

Cannot assign to 'self' outside of a method in the init family
4

3 回答 3

3

setAtScreenCenter是一个实例消息。您必须将其发送到现有的UIActivityIndicatorView. 你为什么要尝试在这种方法中创建一个 UIActivityIndicatorView的?

试试这个:

self.frame = wheelR;

您可能还对坐标的工作方式感到困惑。视图的框架相对于其父视图的坐标系。它与屏幕坐标系无关。即使您的超级视图是全屏的,它的坐标系也会与屏幕的坐标系不同,但只有一个方向。您可以像这样转换坐标:

CGRect frame = [UIScreen mainScreen].applicationFrame;
CGPoint p = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
p = [self.window convertPoint:p fromWindow:nil]; // convert screen -> window
p = [self.superview convertPoint:p fromView:nil]; // convert window -> my superview
self.center = p;
于 2012-12-20T09:42:11.593 回答
2

警告不仅仅是迂腐,编译器实际上是在拯救你自己。您应该声明要返回的新 UIActivityIndi​​cator var,而不是尝试分配给 self. 该方法的更好版本如下所示:

+(UIActivityIndicatorView *)setAtScreenCenter{
    CGRect r = [UIScreen mainScreen].applicationFrame;
    CGRect wheelR = CGRectMake(r.size.width / 2 - 12, r.size.height / 2 - 12, 24, 24);
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:wheelR];
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    indicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                  UIViewAutoresizingFlexibleRightMargin |
                                  UIViewAutoresizingFlexibleTopMargin |
                                  UIViewAutoresizingFlexibleBottomMargin);
    return indicator;
}

作为一个类方法,我们不再需要处理赋值给 self 的问题,而且你总是可以保证满足方法的返回类型(如果这是一个真正的初始化器,它会返回id以便于子类化)。

于 2012-12-20T09:41:26.740 回答
1

这不是真正的 init 方法。因此,您不应该在这里分配给 self 。只需跳过该行并将该方法设为 void 方法(无返回值)。

@implementation UIActivityIndicatorView(Normalize)

-(void) setAtScreenCenter{
    CGRect r = [UIScreen mainScreen].applicationFrame;
    CGRect wheelR = CGRectMake(r.size.width / 2 - 12, r.size.height / 2 - 12, 24, 24);
    self.frame = wheelR;
    self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);

}
@end

现在你可以在现有的 UIActivityIndi​​catorView 上调用它来设置你想要的位置:

[activityWheel setAtScreenCenter];
于 2012-12-20T09:42:23.027 回答