Skip to content

Commit

Permalink
Merge branch 'master' into pr/7
Browse files Browse the repository at this point in the history
# Conflicts:
#	TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m
  • Loading branch information
tingxins committed Dec 5, 2016
1 parent ffb6c53 commit 1e340b6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 45 deletions.
3 changes: 2 additions & 1 deletion TXScrollLabelView/TXScrollLabelView.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
// Created by tingxins on 2/23/16.
// Copyright © 2016 tingxins. All rights reserved.
// Welcome to my blog: https://tingxins.com
// 滚动视图

#define TX_DEPRECATED_METHODS(explain) __attribute__((deprecated(explain)))
Expand Down Expand Up @@ -48,7 +49,7 @@ typedef NS_ENUM(NSInteger, TXScrollLabelViewType) {
@property (copy, nonatomic) NSString *scrollTitle;
/** 滚动类型 */
@property (assign, nonatomic) TXScrollLabelViewType scrollType;
/** 滚动速率([1, 10]),单位秒s */
/** 滚动速率([0, 10]),单位秒s,建议在初始化方法中设置该属性*/
@property (assign, nonatomic) NSTimeInterval scrollVelocity;
/** 文本颜色 */
@property (strong, nonatomic) UIColor *scrollTitleColor;
Expand Down
71 changes: 56 additions & 15 deletions TXScrollLabelView/TXScrollLabelView.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ - (instancetype)initWithTitle:(NSString *)scrollTitle
options:(UIViewAnimationOptions)options
inset:(UIEdgeInsets)inset {
if (self = [super init]) {
self.scrollTitle = scrollTitle;
self.scrollType = scrollType;
_scrollTitle = scrollTitle;
_scrollType = scrollType;
self.scrollVelocity = scrollVelocity;
self.options = options;
_options = options;
_scrollInset = inset;
}
return self;
Expand Down Expand Up @@ -225,7 +225,20 @@ + (instancetype)scrollWithTitle:(NSString *)scrollTitle
}

#pragma mark - Deprecated Getter & Setter Methods
/** WILL BE REMOVED IN FUTURE */
/*************WILL BE REMOVED IN FUTURE.****************************/

- (void)setTx_scrollTitle:(NSString *)tx_scrollTitle {
self.scrollTitle = tx_scrollTitle;
}

- (void)setTx_scrollType:(TXScrollLabelViewType)tx_scrollType {
self.scrollType = tx_scrollType;
}

- (void)setTx_scrollVelocity:(NSTimeInterval)tx_scrollVelocity {
self.scrollVelocity = tx_scrollVelocity;
}

- (void)setTx_scrollContentSize:(CGRect)tx_scrollContentSize{
_tx_scrollContentSize = tx_scrollContentSize;
self.frame = _tx_scrollContentSize;
Expand All @@ -234,14 +247,29 @@ - (void)setTx_scrollContentSize:(CGRect)tx_scrollContentSize{
- (void)setTx_scrollTitleColor:(UIColor *)tx_scrollTitleColor {
self.scrollTitleColor = tx_scrollTitleColor;
}
/*************ALL ABOVE.*******************************************/


#pragma mark - Getter & Setter Methods

- (void)setScrollTitle:(NSString *)scrollTitle {
_scrollTitle = scrollTitle;
// self.scrollArray = nil;
[self resetScrollLabelView];
}

- (void)setScrollType:(TXScrollLabelViewType)scrollType {
if (_scrollType == scrollType) return;

_scrollType = scrollType;
self.scrollVelocity = _scrollVelocity;
[self resetScrollLabelView];
}

- (void)setScrollVelocity:(NSTimeInterval)scrollVelocity {
CGFloat velocity = scrollVelocity;
if (scrollVelocity < 1) {
velocity = 1;
if (scrollVelocity < 0.1) {
velocity = 0.1;
}else if (scrollVelocity > 10) {
velocity = 10;
}
Expand All @@ -251,7 +279,11 @@ - (void)setScrollVelocity:(NSTimeInterval)scrollVelocity {
}else {
_scrollVelocity = velocity;
}

}

- (UIViewAnimationOptions)options {
if (_options) return _options;
return _options = UIViewAnimationOptionCurveEaseInOut;
}

- (void)setScrollTitleColor:(UIColor *)scrollTitleColor {
Expand All @@ -274,15 +306,11 @@ - (CGFloat)scrollSpace {
return 0.f;
}

- (UIViewAnimationOptions)options {
if (_options) return _options;
return _options = UIViewAnimationOptionCurveEaseInOut;
}

- (NSArray *)scrollArray {
if (_scrollArray) return _scrollArray;
return _scrollArray = [self getSeparatedLinesFromLabel];
}

- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self setupSubviewsLayout];
Expand All @@ -307,6 +335,12 @@ - (UIFont *)font {
}

#pragma mark - Custom Methods
/** 重置滚动视图 */
- (void)resetScrollLabelView {
[self endScrolling];//停止滚动
[self setupSubviewsLayout];//重新布局
[self beginScrolling];//开始滚动
}

- (void)setupTextColor:(UIColor *)color {
self.upLabel.textColor = color;
Expand Down Expand Up @@ -391,6 +425,8 @@ - (void)setupSubviewsLayout {
#pragma mark - Scrolling Operation Methods

- (void)beginScrolling {
if (!self.scrollTitle.length) return;

[self endScrolling];

if (_scrollType == TXScrollLabelViewTypeFlipRepeat || _scrollType == TXScrollLabelViewTypeFlipNoRepeat) {
Expand All @@ -407,15 +443,17 @@ - (void)beginScrolling {
- (void)endScrolling {
[self.scrollTimer invalidate];
self.scrollTimer = nil;
self.scrollArray = nil;
}

- (void)pauseScrolling {
[self.scrollTimer invalidate];
self.scrollTimer = nil;
[self endScrolling];
}

//开始计时
- (void)startWithVelocity:(NSTimeInterval)velocity {
if (!self.scrollTitle.length) return;

__weak typeof(self) weakSelf = self;
self.scrollTimer = [NSTimer tx_scheduledTimerWithTimeInterval:velocity repeat:YES block:^(NSTimer *timer) {
TXScrollLabelView *strongSelf = weakSelf;
Expand Down Expand Up @@ -499,10 +537,13 @@ - (void)flipAnimationWithDelay:(NSTimeInterval)delay {
}

- (void)flipNoCleAnimationWithDelay:(NSTimeInterval)delay {
if (!self.scrollArray.count) return;

static int count = 0;
if (count >= self.scrollArray.count) count = 0;
self.upLabel.text = self.scrollArray[count];
count ++;
if (count == self.scrollArray.count) count = 0;
if (count >= self.scrollArray.count) count = 0;
self.downLabel.text = self.scrollArray[count];
[self flipAnimationWithDelay:delay];
}
Expand Down
1 change: 1 addition & 0 deletions TXScrollLabelView/UIView+TXFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by tingxins on 9/1/16.
// Copyright © 2016 tingxins. All rights reserved.
// Welcome to my blog: https://tingxins.com
//

#import <UIKit/UIKit.h>
Expand Down
69 changes: 40 additions & 29 deletions TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

#import "ViewController.h"
#import "TXScrollLabelView.h"
#import "NSString+AttributedString.h"

@interface ViewController ()
@interface ViewController ()<TXScrollLabelViewDelegate>

@property (weak, nonatomic) TXScrollLabelView *scrollLabelView;

Expand All @@ -35,34 +34,46 @@ - (void)setViewInfos {

- (void)setSubviews {

for (int options = 0; options < 4; ++ options) {
NSString *scrollTitle = @"If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers";
//options 是 TXScrollLabelViewType 枚举类型, 此处仅为了方便举例
TXScrollLabelView *scrollLabelView = [TXScrollLabelView scrollWithTitle:scrollTitle type:options velocity:3 options:UIViewAnimationOptionTransitionFlipFromTop];
[self.view addSubview:scrollLabelView];

//布局(Required)
scrollLabelView.frame = CGRectMake(50, 100 * (options + 0.7), 300, 30);

//偏好(Optional)
scrollLabelView.tx_centerX = [UIScreen mainScreen].bounds.size.width * 0.5;
scrollLabelView.scrollInset = UIEdgeInsetsMake(0, 10 , 0, 10);
scrollLabelView.scrollSpace = 10;
scrollLabelView.font = [UIFont systemFontOfSize:15];
scrollLabelView.textAlignment = NSTextAlignmentCenter;
scrollLabelView.backgroundColor = [UIColor blackColor];
scrollLabelView.layer.cornerRadius = 5;

//开始滚动
[scrollLabelView beginScrolling];
self.scrollLabelView = scrollLabelView;

if (options == 0) {
NSMutableAttributedString *tempString = [@"2.1.1:本人使用了九宫格布局,为了方便大家,我给大家提供一个链接地址:(暂时还未整理好,需要的联系我qq154158462 请先关注这个博客哦,不然不通过哟),这个就是直接上面的UI效果,很方便的。" setAttributedWithIdentifyStringArray:@[@"",@"使用了",@"",@"UI",@"方便",@"链接",@"qq"] color:[UIColor redColor] font:nil];
[scrollLabelView setupAttributeTitle:tempString];
}
}
[self addWith:TXScrollLabelViewTypeLeftRight velocity:0.4];

[self addWith:TXScrollLabelViewTypeUpDown velocity:1];

[self addWith:TXScrollLabelViewTypeFlipRepeat velocity:2];

[self addWith:TXScrollLabelViewTypeFlipNoRepeat velocity:2];
}

- (void)addWith:(TXScrollLabelViewType)type velocity:(CGFloat)velocity {
/** Step1: 滚动文字 */
NSString *scrollTitle = @"If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers";

/** Step2: 创建 ScrollLabelView */
TXScrollLabelView *scrollLabelView = [TXScrollLabelView scrollWithTitle:scrollTitle type:type velocity:velocity options:UIViewAnimationOptionCurveEaseInOut];

/** Step3: 设置代理进行回调 */
scrollLabelView.scrollLabelViewDelegate = self;

/** Step4: 布局(Required) */
scrollLabelView.frame = CGRectMake(50, 100 * (type + 0.7), 300, 30);

[self.view addSubview:scrollLabelView];

//偏好(Optional), Preference,if you want.
scrollLabelView.tx_centerX = [UIScreen mainScreen].bounds.size.width * 0.5;
scrollLabelView.scrollInset = UIEdgeInsetsMake(0, 10 , 0, 10);
scrollLabelView.scrollSpace = 10;
scrollLabelView.font = [UIFont systemFontOfSize:15];
scrollLabelView.textAlignment = NSTextAlignmentCenter;
scrollLabelView.backgroundColor = [UIColor blackColor];
scrollLabelView.layer.cornerRadius = 5;

/** Step5: 开始滚动(Start scrolling!) */
[scrollLabelView beginScrolling];

}

- (void)scrollLabelView:(TXScrollLabelView *)scrollLabelView didClickWithText:(NSString *)text{
NSLog(@"%@",text);
}

@end

0 comments on commit 1e340b6

Please sign in to comment.