Skip to content

Commit 68bddda

Browse files
committed
add Demo.
1 parent cc5ef43 commit 68bddda

File tree

23 files changed

+3806
-0
lines changed

23 files changed

+3806
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// TXScrollLabelView.h
3+
//
4+
// Created by tingxins on 2/23/16.
5+
// Copyright © 2016 tingxins. All rights reserved.
6+
// 滚动视图
7+
8+
typedef enum {
9+
TXScrollLabelViewType_LR,
10+
TXScrollLabelViewType_UD,
11+
TXScrollLabelViewType_Flip,
12+
TXScrollLabelViewType_FlipNoCle
13+
}TXScrollLabelViewType;
14+
15+
#import <UIKit/UIKit.h>
16+
17+
@interface TXScrollLabelView : UIScrollView
18+
19+
#pragma mark - Property
20+
/** 滚动文字 */
21+
@property (copy, nonatomic) NSString *scrollTitle;
22+
/** 滚动类型 */
23+
@property (assign, nonatomic) TXScrollLabelViewType scrollType;
24+
/** 滚动速率 */
25+
@property (assign, nonatomic) NSTimeInterval scrollVelocity;
26+
27+
#pragma mark - Methods
28+
/**
29+
* 开始滚动
30+
*/
31+
- (void) beginScrolling;
32+
/**
33+
* 停止滚动
34+
*/
35+
- (void) stopScrolling;
36+
37+
/**
38+
* 暂停滚动
39+
*/
40+
- (void) pauseScrolling;
41+
42+
@end
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
//
2+
// TXScrollLabelView.m
3+
//
4+
// Created by tingxins on 2/23/16.
5+
// Copyright © 2016 tingxins. All rights reserved.
6+
//
7+
#define TXScrollLabelFont [UIFont systemFontOfSize:14]
8+
#import "TXScrollLabelView.h"
9+
#import "UIView+TXFrame.h"
10+
#import <CoreText/CoreText.h>
11+
12+
@interface TXScrollLabelView ()
13+
14+
@property (weak, nonatomic) UILabel *scrollLabel0;
15+
16+
@property (weak, nonatomic) UILabel *scrollLabel1;
17+
18+
@property (weak, nonatomic) UITextView *scrollTextView;
19+
20+
@property (strong, nonatomic) NSTimer *scrollTimer;
21+
22+
@property (strong, nonatomic) NSArray *scrollArray;
23+
24+
@end
25+
26+
@implementation TXScrollLabelView
27+
28+
- (NSArray *)scrollArray {
29+
if (_scrollArray) return _scrollArray;
30+
return _scrollArray = [self getSeparatedLinesFromLabel];
31+
}
32+
33+
- (instancetype)initWithFrame:(CGRect)frame {
34+
if (self = [super initWithFrame:frame]) {
35+
self.backgroundColor = [UIColor blackColor];
36+
UILabel *scrollLabel0 = [[UILabel alloc]init];
37+
scrollLabel0.numberOfLines = 0;
38+
self.scrollLabel0 = scrollLabel0;
39+
self.scrollLabel0.font = TXScrollLabelFont;
40+
self.scrollLabel0.textColor = [UIColor whiteColor];
41+
[self addSubview:scrollLabel0];
42+
43+
UILabel *scrollLabel1 = [[UILabel alloc]init];
44+
scrollLabel1.numberOfLines = 0;
45+
self.scrollLabel1 = scrollLabel1;
46+
self.scrollLabel1.font = TXScrollLabelFont;
47+
self.scrollLabel1.textColor = [UIColor whiteColor];
48+
[self addSubview:scrollLabel1];
49+
50+
scrollLabel0.lineBreakMode = NSLineBreakByWordWrapping;
51+
scrollLabel1.lineBreakMode = NSLineBreakByWordWrapping;
52+
scrollLabel0.textAlignment = NSTextAlignmentCenter;
53+
scrollLabel1.textAlignment = NSTextAlignmentCenter;
54+
self.scrollEnabled = NO;
55+
}
56+
return self;
57+
}
58+
59+
- (void)setScrollTitle:(NSString *)scrollTitle {
60+
_scrollTitle = [scrollTitle copy];
61+
62+
switch (self.scrollType) {
63+
case TXScrollLabelViewType_LR:
64+
{
65+
CGSize scrollLabelS = [self.scrollTitle boundingRectWithSize:CGSizeMake(0, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: TXScrollLabelFont} context:nil].size;
66+
self.scrollLabel0.frame = CGRectMake(10, 0, scrollLabelS.width, 30);
67+
self.scrollLabel0.text = self.scrollTitle;
68+
69+
self.scrollLabel1.frame = CGRectMake(CGRectGetMaxX(self.scrollLabel0.frame) + 20, 0, scrollLabelS.width, 30);
70+
self.scrollLabel1.text = self.scrollTitle;
71+
self.contentSize = CGSizeMake(scrollLabelS.width * 2 + 40, scrollLabelS.height);
72+
CGFloat maxWidth = 200;
73+
self.bounds = CGRectMake(0, 0, MIN(maxWidth, scrollLabelS.width), 30);
74+
}
75+
break;
76+
77+
case TXScrollLabelViewType_UD:
78+
{
79+
80+
CGSize scrollLabelS = [self.scrollTitle boundingRectWithSize:CGSizeMake(200, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: TXScrollLabelFont} context:nil].size;
81+
self.scrollLabel0.frame = CGRectMake(0, 0, scrollLabelS.width, scrollLabelS.height);
82+
self.scrollLabel0.text = self.scrollTitle;
83+
84+
self.scrollLabel1.frame = CGRectMake(0, CGRectGetMaxY(self.scrollLabel0.frame), scrollLabelS.width, scrollLabelS.height);
85+
self.scrollLabel1.text = self.scrollTitle;
86+
self.contentSize = CGSizeMake(scrollLabelS.width, scrollLabelS.height * 2);
87+
self.bounds = CGRectMake(0, 0, scrollLabelS.width, 30);
88+
}
89+
break;
90+
91+
case TXScrollLabelViewType_Flip:
92+
{
93+
94+
CGSize scrollLabelS = [self.scrollTitle boundingRectWithSize:CGSizeMake(200, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: TXScrollLabelFont} context:nil].size;
95+
self.scrollLabel0.frame = CGRectMake(0, 0, scrollLabelS.width, 30);
96+
self.scrollLabel0.text = self.scrollTitle;
97+
98+
self.scrollLabel1.frame = CGRectMake(0, CGRectGetMaxY(self.scrollLabel0.frame), scrollLabelS.width, 30);;
99+
self.scrollLabel1.text = self.scrollTitle;
100+
self.contentSize = CGSizeMake(scrollLabelS.width, scrollLabelS.height * 2);
101+
self.bounds = CGRectMake(0, 0, scrollLabelS.width, 30);
102+
}
103+
break;
104+
105+
106+
case TXScrollLabelViewType_FlipNoCle:
107+
{
108+
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
109+
paraStyle.lineSpacing = 10;
110+
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:_scrollTitle];
111+
[attributedStr addAttributes:@{NSFontAttributeName:TXScrollLabelFont, NSParagraphStyleAttributeName:paraStyle} range:NSMakeRange(0, attributedStr.length)];
112+
113+
CGSize scrollLabelS = [attributedStr boundingRectWithSize:CGSizeMake(self.tx_width, 0) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
114+
115+
self.scrollLabel0.frame = CGRectMake(0, 0, scrollLabelS.width, self.tx_height);
116+
self.scrollLabel1.frame = CGRectMake(0, CGRectGetMaxY(self.scrollLabel0.frame), scrollLabelS.width, self.tx_height);
117+
118+
CGFloat width = scrollLabelS.width;
119+
if (!self.tx_width) {
120+
self.tx_width = width;
121+
}
122+
self.contentSize = CGSizeMake(self.tx_width, scrollLabelS.height * 2);
123+
self.bounds = CGRectMake(0, 0, self.tx_width, self.tx_height);
124+
}
125+
break;
126+
127+
default:
128+
break;
129+
}
130+
}
131+
132+
- (void)beginScrolling {
133+
[self stopScrolling];
134+
self.scrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateScrolling) userInfo:nil repeats:YES];
135+
[[NSRunLoop mainRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
136+
}
137+
138+
- (void)stopScrolling {
139+
[self.scrollTimer invalidate];
140+
self.scrollTimer = nil;
141+
}
142+
143+
- (void)pauseScrolling {
144+
[self.scrollTimer invalidate];
145+
self.scrollTimer = nil;
146+
}
147+
148+
- (void)updateScrolling {
149+
switch (self.scrollType) {
150+
case TXScrollLabelViewType_LR:
151+
{
152+
if (self.contentSize.width * 0.5 == self.contentOffset.x || self.contentOffset.x > self.contentSize.width * 0.5) {
153+
[self stopScrolling];
154+
self.contentOffset = CGPointMake(2, 0);//x增加偏移量,防止卡顿
155+
[self beginScrolling];
156+
return;
157+
}
158+
self.contentOffset = CGPointMake(self.contentOffset.x + 1, self.contentOffset.y);
159+
160+
}
161+
break;
162+
163+
case TXScrollLabelViewType_UD:
164+
{
165+
if (self.contentOffset.y > self.scrollLabel0.frame.size.height) {
166+
[self stopScrolling];
167+
self.contentOffset = CGPointMake(0, 2);//y增加偏移量,防止卡顿
168+
[self beginScrolling];
169+
return;
170+
}
171+
self.contentOffset = CGPointMake(self.contentOffset.x, self.contentOffset.y + 1);
172+
173+
}
174+
break;
175+
176+
case TXScrollLabelViewType_Flip:
177+
{
178+
179+
[self stopScrolling];
180+
self.scrollTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateScrolling) userInfo:nil repeats:YES];
181+
[[NSRunLoop mainRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
182+
183+
[self flipAnimationWithDelay:0];
184+
}
185+
break;
186+
187+
case TXScrollLabelViewType_FlipNoCle:
188+
{
189+
190+
[self stopScrolling];
191+
NSTimeInterval velocity = self.scrollVelocity ? self.scrollVelocity : 2;
192+
193+
self.scrollTimer = [NSTimer scheduledTimerWithTimeInterval:velocity target:self selector:@selector(updateScrolling) userInfo:nil repeats:YES];
194+
[[NSRunLoop mainRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
195+
196+
[self flipNoCleAnimationWithDelay:velocity];
197+
}
198+
break;
199+
200+
default:
201+
break;
202+
}
203+
}
204+
205+
- (void)flipAnimationWithDelay:(NSTimeInterval)delay {
206+
[UIView transitionWithView:self.scrollLabel0 duration:delay * 0.5 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
207+
self.scrollLabel0.tx_bottom = 0;
208+
[UIView transitionWithView:self.scrollLabel0 duration:delay * 0.5 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
209+
self.scrollLabel1.tx_y = 0;
210+
} completion:^(BOOL finished) {
211+
self.scrollLabel0.tx_y = self.tx_height;
212+
UILabel *tempLabel = self.scrollLabel0;
213+
self.scrollLabel0 = self.scrollLabel1;
214+
self.scrollLabel1 = tempLabel;
215+
}];
216+
} completion:nil];
217+
}
218+
219+
- (void)flipNoCleAnimationWithDelay:(NSTimeInterval)delay {
220+
static int count = 0;
221+
222+
self.scrollLabel0.text = self.scrollArray[count];
223+
224+
count ++;
225+
226+
if (count == self.scrollArray.count) count = 0;
227+
228+
self.scrollLabel1.text = self.scrollArray[count];
229+
230+
[self flipAnimationWithDelay:delay];
231+
}
232+
233+
#pragma mark - 文字分行
234+
235+
-(NSArray *)getSeparatedLinesFromLabel {
236+
237+
NSString *text = _scrollTitle;
238+
UIFont *font = [UIFont systemFontOfSize:15];
239+
240+
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
241+
242+
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
243+
244+
[attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
245+
246+
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
247+
248+
CGMutablePathRef path = CGPathCreateMutable();
249+
250+
CGPathAddRect(path, NULL, CGRectMake(0,0,self.tx_width,100000));
251+
252+
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
253+
254+
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
255+
256+
NSMutableArray *linesArray = [[NSMutableArray alloc]init];
257+
258+
for (id line in lines) {
259+
260+
CTLineRef lineRef = (__bridge CTLineRef )line;
261+
262+
CFRange lineRange = CTLineGetStringRange(lineRef);
263+
264+
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
265+
266+
NSString *lineString = [text substringWithRange:range];
267+
268+
[linesArray addObject:lineString];
269+
}
270+
return (NSArray *)linesArray;
271+
}
272+
273+
@end

TXScrollLabelView/UIView+TXFrame.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// UIView+TXFrame.h
3+
// TXSwipeTableViewTest
4+
//
5+
// Created by tingxins on 9/1/16.
6+
// Copyright © 2016 tingxins. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIView (TXFrame)
12+
/** 设置x值 */
13+
@property (assign, nonatomic) CGFloat tx_x;
14+
/** 设置y值 */
15+
@property (assign, nonatomic) CGFloat tx_y;
16+
/** 设置width */
17+
@property (assign, nonatomic) CGFloat tx_width;
18+
/** 设置height */
19+
@property (assign, nonatomic) CGFloat tx_height;
20+
/** 设置size */
21+
@property (assign, nonatomic) CGSize tx_size;
22+
/** 设置origin */
23+
@property (assign, nonatomic) CGPoint tx_origin;
24+
/** 设置center */
25+
@property (assign, nonatomic) CGPoint tx_center;
26+
/** 设置center.x */
27+
@property (assign, nonatomic) CGFloat tx_centerX;
28+
/** 设置center.y */
29+
@property (assign, nonatomic) CGFloat tx_centerY;
30+
/** 设置bottom */
31+
@property (assign, nonatomic) CGFloat tx_bottom;
32+
@end

0 commit comments

Comments
 (0)