创建一个基于UIView的类
.h文件中
.h文件中
#import <UIKit/UIKit.h>
@class GYChangeTextView;
@protocol GYChangeTextViewDelegate <NSObject>
//点击公告方法
- (void)gyChangeTextView:(GYChangeTextView *)textView didTapedAtIndex:(NSInteger)index;
@end
@interface GYChangeTextView : UIView
@property (nonatomic, assign) id<GYChangeTextViewDelegate> delegate;
- (void)animationWithTexts:(NSArray *)textAry;
- (void)stopAnimation;
@end
.m文件中
#import "GYChangeTextView.h"
#define DEALY_WHEN_TITLE_IN_MIDDLE 3.0
#define DEALY_WHEN_TITLE_IN_BOTTOM 0.0
typedef NS_ENUM(NSUInteger, GYTitlePosition) {
GYTitlePositionTop = 1,
GYTitlePositionMiddle = 2,
GYTitlePositionBottom = 3
};
@interface GYChangeTextView ()
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) NSArray *contentsAry;
@property (nonatomic, assign) CGPoint topPosition;
@property (nonatomic, assign) CGPoint middlePosition;
@property (nonatomic, assign) CGPoint bottomPosition;
/*
*1.控制延迟时间,当文字在中间时,延时时间长一些,如5秒,这样可以让用户浏览清楚内容;
*2.当文字隐藏在底部的时候,不需要延迟,这样衔接才流畅;
*3.通过上面的宏定义去更改需要的值
*/
@property (nonatomic, assign) CGFloat needDealy;
@property (nonatomic, assign) NSInteger currentIndex; /*当前播放到那个标题了*/
@property (nonatomic, assign) BOOL shouldStop; /*是否停止*/
@end
@implementation GYChangeTextView
//重新系统方法
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.topPosition = CGPointMake(self.frame.size.width/2, self.frame.size.height/2-23);
self.middlePosition = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
self.bottomPosition = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+23);
self.shouldStop = NO;
UIImageView *advertiView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 20, 20)];
advertiView.image = [UIImage imageNamed:@"advertis"];
[self addSubview:advertiView];
_textLabel = [[UILabel alloc] init];
_textLabel.layer.bounds = CGRectMake(0, 0, CGRectGetWidth(frame)-100, CGRectGetHeight(frame));
_textLabel.layer.position = self.middlePosition;
_textLabel.textAlignment = NSTextAlignmentLeft;
[self addSubview:_textLabel];
self.clipsToBounds = YES; /*保证文字不跑出视图*/
self.needDealy = DEALY_WHEN_TITLE_IN_MIDDLE; /*控制第一次显示时间*/
self.currentIndex = 0;
}
return self;
}
//给动画添加内容
- (void)animationWithTexts:(NSArray *)textAry {
self.contentsAry = textAry;
self.textLabel.text = [textAry objectAtIndex:0];
[self startAnimation];
}
//开始动画
- (void)startAnimation {
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3 delay:self.needDealy options:UIViewAnimationOptionCurveEaseInOut animations:^{
if ([weakSelf currentTitlePosition] == GYTitlePositionMiddle) {
weakSelf.textLabel.layer.position = weakSelf.topPosition;
} else if ([weakSelf currentTitlePosition] == GYTitlePositionBottom) {
weakSelf.textLabel.layer.position = weakSelf.middlePosition;
}
} completion:^(BOOL finished) {
if ([weakSelf currentTitlePosition] == GYTitlePositionTop) {
weakSelf.textLabel.layer.position = weakSelf.bottomPosition;
weakSelf.needDealy = DEALY_WHEN_TITLE_IN_BOTTOM;
weakSelf.currentIndex ++;
weakSelf.textLabel.text = [weakSelf.contentsAry objectAtIndex:[weakSelf realCurrentIndex]];
} else {
weakSelf.needDealy = DEALY_WHEN_TITLE_IN_MIDDLE;
}
if (!weakSelf.shouldStop) {
[weakSelf startAnimation];
} else { //停止动画后,要设置label位置和label显示内容
weakSelf.textLabel.layer.position = weakSelf.middlePosition;
weakSelf.textLabel.text = [weakSelf.contentsAry objectAtIndex:[weakSelf realCurrentIndex]];
}
}];
}
//停止动画
- (void)stopAnimation {
self.shouldStop = YES;
}
- (NSInteger)realCurrentIndex {
return self.currentIndex % [self.contentsAry count];
}
- (GYTitlePosition)currentTitlePosition {
if (self.textLabel.layer.position.y == self.topPosition.y) {
return GYTitlePositionTop;
} else if (self.textLabel.layer.position.y == self.middlePosition.y) {
return GYTitlePositionMiddle;
}
return GYTitlePositionBottom;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if ([self.delegate respondsToSelector:@selector(gyChangeTextView:didTapedAtIndex:)]) {
[self.delegate gyChangeTextView:self didTapedAtIndex:[self realCurrentIndex]];
}
}
@end
在控制器里调用
GYChangeTextView *tView = [[GYChangeTextView alloc] initWithFrame:CGRectMake(0, JYZScreenH*0.3, JYZScreenW, 30)];
tView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
tView.delegate = self;
[view addSubview:tView];
self.tView = tView;
[self.tView animationWithTexts:self.advertisArr];
#pragma mark 公告视图代理
- (void)gyChangeTextView:(GYChangeTextView *)textView didTapedAtIndex:(NSInteger)index
{
}