IBTScrollLoadMoreView.m 3.09 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
//
//  IBTScrollLoadMoreView.m
//  TableViewRefresh
//
//  Created by Xummer on 14-3-27.
//  Copyright (c) 2014年 Xummer. All rights reserved.
//

#import "IBTScrollLoadMoreView.h"

@interface IBTScrollLoadMoreView ()

@property (nonatomic, strong) UILabel *loadMoreLabel;
@property (nonatomic, strong) UIActivityIndicatorView *indicator;

@end

@implementation IBTScrollLoadMoreView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    [self setupDefualtText];
    
    self.loadMoreLabel = [[UILabel alloc] init];
    _loadMoreLabel.backgroundColor = [UIColor clearColor];
    _loadMoreLabel.textAlignment = NSTextAlignmentCenter;
    _loadMoreLabel.font = [UIFont systemFontOfSize:14];
    _loadMoreLabel.textColor = [UIColor grayColor];
    _loadMoreLabel.text = _loadMoreText;
    
    [self addSubview:_loadMoreLabel];
    
    self.indicator =
    [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self addSubview:_indicator];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect frame = CGRectInset(self.bounds, 20, 7);
    _loadMoreLabel.frame = frame;
    
    frame = _indicator.frame;
    frame.origin =
    CGPointMake(50, (CGRectGetHeight(self.bounds) - CGRectGetHeight(_indicator.frame))*.5f );
    _indicator.frame = frame;
    
}

- (void)setupDefualtText {
    self.loadMoreText = [IBTCommon localizableString:@"Pull up to load more"];
    self.dragToLoadText = [IBTCommon localizableString:@"Drop to load more"];
    self.loadingText = [IBTCommon localizableString:@"Loading..."];
    self.finishedText = @""; //@"Load finished";
    self.failedText = @""; //@"Load failed";
    self.noMoreText = @""; //@"No more";
}

- (void)updateState:(LoadMoreState)state {
    if (state == _currentState) {
        return;
    }
    NSString *textStr;
    BOOL needAnimate = NO;
    switch (state) {
        case kLoadStateNormal:
            textStr = _loadMoreText;
            break;
        case kLoadStateDraging:
            textStr = _dragToLoadText;
            break;
        case kLoadStateLoading:
            needAnimate = YES;
            textStr = _loadingText;
            break;
        case kLoadStateFinished:
            textStr = _finishedText;
            break;
        case kLoadStateFailed:
            textStr = _failedText;
            break;
        case kLoadStateNoMore:
            textStr = _noMoreText;
            break;
        default:
            break;
    }
    
    _loadMoreLabel.text = textStr;
    if (needAnimate) {
        if (![_indicator isAnimating]) {
            [_indicator startAnimating];
        }
    }
    else {
        if ([_indicator isAnimating]) {
            [_indicator stopAnimating];
        }
    }
    
    _currentState = state;
    
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    _loadMoreLabel.textColor = textColor;
}

- (void)setLoadMoreText:(NSString *)loadMoreText {
    _loadMoreText = loadMoreText;
    _loadMoreLabel.text = loadMoreText;
}


@end