单军华
2017-07-12 20d1260d26b028897f3c0935c12fc35aa37b2e93
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
//  HBdansView.m
//  弹幕
//
//  Created by 伍宏彬 on 15/10/14.
//  Copyright (c) 2015年 伍宏彬. All rights reserved.
//
 
#import "HBdansView.h"
#import "HBdansLable.h"
#import "NoticeModel.h"
 
@interface HBdansView()<HBdansLableDelegate>
 
@property (nonatomic, strong) NSMutableSet *randomSet;
 
 
@end
 
@implementation HBdansView
 
#define defaultH 25
 
- (instancetype)initDansViewFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.randomMutableArray = [[NSMutableArray alloc] init];
        [self setInit];
    }
    return self;
}
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setInit];
    }
    return self;
}
#pragma mark - initIvar
- (void)setInit
{
    self.textBackColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    self.countInScreen = 1;
    self.clipsToBounds = YES;
    self.currentIndex = 0;
}
- (void)starDans
{
    if (!self.randomMutableArray.count) return;
    
    NoticeModel * hotelSelModel = [NoticeModel mj_objectWithKeyValues:[self.randomMutableArray objectAtIndex:self.currentIndex]];
    
    [self addRandomText:hotelSelModel.content];
    self.currentIndex++;
    //[self addRandomText:[self.randomMutableArray firstObject]];
    //[self.randomMutableArray removeObjectAtIndex:0];
}
 
- (void)addRandomText:(NSString *)randomText
{
    if (randomText.length)
    {
        if (![self dequeRandomLable:randomText])
        {
            [self.randomMutableArray addObject:randomText];
        }
    }
}
#pragma mark - CustomMethod
- (HBdansLable *)dequeRandomLable:(NSString *)text
{
    HBdansLable *randomLable = [self.randomSet anyObject];
    
    if (!randomLable) return nil;
    
    [self setRandomStyle:randomLable];
    
    [self addSubview:randomLable];
    
    [self.randomSet removeObject:randomLable];
        
    randomLable.text = text;
        
    return randomLable;
}
- (void)setRandomStyle:(HBdansLable *)lable
{
    lable.layer.cornerRadius = self.roundVaule;
    lable.layer.borderColor = self.lineColor.CGColor;
    lable.layer.borderWidth = self.lineWidth;
    lable.backgroundColor = self.textBackColor;
    lable.textColor = self.textColor;
}
- (CGRect)randomFrame
{
    
    CGFloat randomW = 10;
    CGFloat randomH = defaultH;
    CGFloat randomX = CGRectGetMaxX(self.frame);
    CGFloat randomY = 0;
    
    return CGRectMake(randomX, randomY, randomW, randomH);
}
#pragma mark - HBdansLableDelegate
- (void)dansLable:(HBdansLable *)dansLable isOutScreen:(BOOL)isOutScreen
{
    if (isOutScreen)
    {
        dansLable.x = CGRectGetMaxX(self.frame);
        
        [dansLable removeFromSuperview];
        [self.randomSet addObject:dansLable];
        
        if (self.randomMutableArray.count)
        {
            // 循环设置
            if(self.currentIndex == self.randomMutableArray.count)
                self.currentIndex = 0;
            
            NoticeModel * hotelSelModel = [NoticeModel mj_objectWithKeyValues:[self.randomMutableArray objectAtIndex:self.currentIndex]];
            
            [self addRandomText:hotelSelModel.content];
            self.currentIndex++;
            
            //[self dequeRandomLable:[self.randomMutableArray firstObject]];
            //[self.randomMutableArray removeObjectAtIndex:0];
            
        }
    }
}
#pragma mark - getter
- (NSMutableArray *)randomMutableArray
{
    if (!_randomMutableArray) {
        _randomMutableArray = [NSMutableArray array];
    }
    return _randomMutableArray;
}
- (NSMutableSet *)randomSet
{
    if (!_randomSet) {
        
        _randomSet = [NSMutableSet set];
        
    }
    return _randomSet;
}
- (void)setCountInScreen:(NSInteger)countInScreen
{
    _countInScreen = countInScreen;
    
    if (self.randomSet.count) [self.randomSet removeAllObjects];
    //    随机
    if (_countInScreen > 10) _countInScreen = 10;
    CGFloat margin = (self.height - _countInScreen * defaultH) / (_countInScreen + 1);
    for (NSInteger i = 0; i < _countInScreen; i++)
    {
        HBdansLable *randomLable = [HBdansLable dansLableFrame:[self randomFrame]];
        randomLable.y = i * (margin + defaultH) + margin;
        randomLable.delegate = self;
        randomLable.layer.masksToBounds = YES;
        [self.randomSet addObject:randomLable];
    }
}
@end