奇怪但很容易理解的错误...... UIActivityIndi​​catorView stopAnimating无效

时间:2015-09-23 08:20:48

标签: ios uiview uiactivityindicatorview

编辑:UIActivityIndi​​catorView认为它不在屏幕上......所有值都显示它不是,但是当你看屏幕时,它就在那里。

所以......这有点让我疯狂。

我正在使用https://github.com/fphilipe/PHFComposeBarView

我正在编辑它,以便在加载时我可以用UIActivityIndi​​catorView代替右边的按钮...

目前,我可以完美地开始动画UIActivityIndi​​catorView ...但是当我必须停止动画时,它不会停止并且它不会消失。

我有hidesWhenStopped = true我知道主线程上发生了一切。

事实上,当我使用printlnNSLog时,我发现UIActivityIndi​​catorView认为它是隐藏的......当它显然不是。

任何人都知道为什么它可能不起作用?

编辑:我甚至尝试通过将UIActivityIndi​​catorView设置为动画并将其添加到视图中或将其从超级视图中删除来缓解此问题...再次,当我需要删除它时,它将无法工作。 ..

- (void)startLoading {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self loadIndicator] startAnimating];

    });
}

- (void)stopLoading {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self loadIndicator] stopAnimating];
    });
}

@synthesize button = _button;
- (UIButton *)button {
    if (!_button) {
        _button = [PHFComposeBarView_Button buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake([self bounds].size.width - kHorizontalSpacing - kButtonRightMargin - kButtonTouchableOverlap,
                                  [self bounds].size.height - kButtonBottomMargin - kButtonHeight,
                                  2 * kButtonTouchableOverlap,
                                  kButtonHeight);
        [_button setFrame:frame];
        [_button setTitleEdgeInsets:UIEdgeInsetsMake(0.5f, 0, 0, 0)];
        [_button setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin];
        [_button setTitle:[self buttonTitle] forState:UIControlStateNormal];

        UIColor *disabledColor = [UIColor colorWithHue:240.0f/360.0f saturation:0.03f brightness:0.58f alpha:1.0f];
        [_button setTitleColor:disabledColor forState:UIControlStateDisabled];
        UIColor *enabledColor = [UIColor colorWithHue:211.0f/360.0f saturation:1.0f brightness:1.0f alpha:1.0f];
        [_button setTitleColor:enabledColor forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(didPressButton) forControlEvents:UIControlEventTouchUpInside];

        UILabel *label = [_button titleLabel];
        [label setFont:[UIFont boldSystemFontOfSize:kFontSize]];
    }

    return _button;
}

@synthesize loadIndicator = _loadIndicator;
- (UIActivityIndicatorView *)loadIndicator {
    if (!_loadIndicator) {

        UIButton *rightButton = [self button];

        _loadIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake([rightButton frame].origin.x + [rightButton frame].size.width / 2.0f - [rightButton frame].size.height / 2.0f, [rightButton frame].origin.y, [rightButton frame].size.height, [rightButton frame].size.height)];

        [_loadIndicator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin];

        _loadIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        _loadIndicator.hidesWhenStopped = true;

    }

    return _loadIndicator;
}

这应该是相关的代码......

在我的ViewController中:

var composeBarView: PHFComposeBarView {
    var viewBounds = self.view
    var frame = CGRectMake(0, viewBounds.frame.height - PHFComposeBarViewInitialHeight, viewBounds.frame.width, PHFComposeBarViewInitialHeight)

    var composeBarView = PHFComposeBarView(frame: frame)

    composeBarView.maxLinesCount = 6
    composeBarView.placeholder = "Write some text"
    composeBarView.buttonTitle = "Reply"
    composeBarView.delegate = self

    composeBarView.buttonTintColor = UIColor(hexString: "056A85")
    composeBarView.textView.backgroundColor = UIColor.whiteColor()

    return composeBarView
}

1 个答案:

答案 0 :(得分:3)

因为您使用的是计算属性,而不是惰性属性,所以每次调用self.composeBarView时都会创建一个新实例。您的代码使用传递的视图开始制作动画,但要停止制作动画,您始终会创建一个显然尚未显示的新视图。

将您的代码更改为:

lazy var composeBarView: PHFComposeBarView = {
    var viewBounds = self.view
    var frame = CGRectMake(0, viewBounds.frame.height - PHFComposeBarViewInitialHeight, viewBounds.frame.width, PHFComposeBarViewInitialHeight)

    var composeBarView = PHFComposeBarView(frame: frame)

    composeBarView.maxLinesCount = 6
    composeBarView.placeholder = "Write some text"
    composeBarView.buttonTitle = "Reply"
    composeBarView.delegate = self

    composeBarView.textView.backgroundColor = UIColor.whiteColor()

    return composeBarView
}()