单军华
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
//
//  HBdansLable.m
//  弹幕
//
//  Created by 伍宏彬 on 15/10/14.
//  Copyright (c) 2015年 伍宏彬. All rights reserved.
//
 
#import "HBdansLable.h"
 
@interface HBdansLable()
{
    HBdansLable *_randomLable;
}
 
@property (nonatomic, strong) CADisplayLink *displayLink;
 
@property (nonatomic, assign) BOOL isStar;
 
@end
 
@implementation HBdansLable
 
 
 
+ (instancetype)dansLableFrame:(CGRect)frame
{
    HBdansLable *dansLable = [[HBdansLable alloc] initWithFrame:frame];
    return dansLable;
    
}
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textAlignment = NSTextAlignmentCenter;
        
        //        self.layer.cornerRadius = self.roundVaule;
        //        self.layer.masksToBounds = YES;
        //
        //        self.layer.borderColor = self.lineColor.CGColor;
        //        self.layer.borderWidth = self.lineWidth;
        
        
        [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}
 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    HBdansLable *dansLable = (HBdansLable *)object;
    
    if ([self.delegate respondsToSelector:@selector(dansLable:isOutScreen:)]) {
        [self.delegate dansLable:dansLable isOutScreen:[self isOutScreen]];
    }
    
    
}
- (void)updateFrame
{
    if (self.isStar)
        self.x -= 1;
}
- (void)starDans
{
    
    self.displayLink.paused = NO;
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    self.isStar = YES;
    
}
- (void)stopDans
{
    if (self.displayLink.isPaused) return;
    self.displayLink.paused = YES;
    self.isStar = NO;
    [self.displayLink invalidate];
    self.displayLink = nil;
    
}
 
- (BOOL)isOutScreen
{
    if (self.x < 0 && ABS(self.x) >= self.width) {
        
        [self stopDans];
        
        return YES;
    }
    
    return NO;
}
 
#pragma mark - getter
- (CADisplayLink *)displayLink
{
    if (!_displayLink) {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateFrame)];
        _displayLink.frameInterval = 1;
    }
    return _displayLink;
}
 
- (void)setText:(NSString *)text
{
    [super setText:text];
    
    CGRect contextFrame = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.height)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]}
                                             context:nil];
    NSString *str = [NSString stringWithFormat:@"%.f",contextFrame.size.width + 10];
    self.width = [str floatValue];
    self.height = contextFrame.size.height + 6;
    
    [self starDans];
}
 
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"frame" context:nil];
}
 
@end