实现上下切换文字效果

哈哈,自己拍的视频制作的gif图,有点low!
效果图

文字滚动
实现思路:首先是要有一个UILabel,数组,定时器。然后在定时器变化的方法里面改变数组的索引,添加动画效果,给UILabel赋值。

HYScrollMessageView.h文件
#import <UIKit/UIKit.h>

@interface HYScrollMessageView : UIView

@property (nonatomic,strong) NSArray *rssArray;
@property (nonatomic,copy) void (^messageClickBlock)();

– (void)makeMsgArr:(NSArray *)rssArray;
– (void)startTimer;
– (void)stopTimer;
@end

HYScrollMessageView.m文件
#import “HYScrollMessageView.h”

@interface HYScrollMessageView()

@property (nonatomic,strong) UILabel *messageLabel;
@property (nonatomic,strong) UIButton *messageBtn;
@property (nonatomic, strong) NSTimer *animationTimer;

@end

static int countInt=0;
@implementation HYScrollMessageView

– (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)
];
_messageLabel.textColor = [UIColor blackColor];
_messageLabel.backgroundColor = [UIColor clearColor];
_messageLabel.font = [UIFont systemFontOfSize:14];
_messageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_messageLabel.numberOfLines = 1;
[self addSubview:_messageLabel];

_messageBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[_messageBtn addTarget:self action:@selector(messageBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_messageBtn];
}
return self;
}

//按钮点击回调
– (void)messageBtnClicked:(UIButton *)sender
{
if (self.messageClickBlock) {
self.messageClickBlock();
}
}

– (void)makeMsgArr:(NSArray *)rssArray
{
if (rssArray.count == 0 || rssArray == nil || rssArray == NULL) {
return;
}

self.rssArray = rssArray;
[self.messageLabel setText:[self.rssArray objectAtIndex:countInt] ];
//设置定时器
[self startTimer];
}

//信息的轮播
-(void)displayNews
{
//增加数组索引
countInt ++;
if (countInt >= [_rssArray count]) {
countInt = 0;
}
//实现动画效果
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.subtype = kCATransitionFromTop;
animation.fillMode = kCAFillModeBackwards;
animation.removedOnCompletion = YES;
animation.type = @”push”;

[self.messageLabel setText:[self.rssArray objectAtIndex:countInt] ];
[self.messageLabel.layer addAnimation:animation forKey:@”animationID”];
}

//启用定时器
– (void)startTimer
{
if (!_animationTimer) {
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(displayNews)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.animationTimer forMode:NSRunLoopCommonModes];
}
}
//关闭定时器
– (void)stopTimer
{
if ([self.animationTimer isValid] == YES)
[self.animationTimer invalidate];
self.animationTimer = nil;
NSLog(@”系统消息定时器停止”);
}

@end

使用时操作
#import “ViewController.h”
#import “HYScrollMessageView.h”
#import “WebViewController.h”

@interface ViewController ()

{
NSArray *msgArray;
}
@property (strong, nonatomic) HYScrollMessageView *messageView;

@end

@implementation ViewController

– (void)viewDidLoad {
[super viewDidLoad];
self.title = @”Message”;
msgArray= [NSArray arrayWithObjects:@”今天天气不错,哈哈”,@”明天有雨,哈哈”,@”后天晴天转多云”,@”前天多云转晴,嘿嘿”, nil];
_messageView = [[HYScrollMessageView alloc]initWithFrame:CGRectMake(50, 100, 200, 25)];
[_messageView makeMsgArr:msgArray];
__weak typeof (self) myself = self;
_messageView.messageClickBlock = ^(){
WebViewController *vc = [[WebViewController alloc] init];
[myself.navigationController pushViewController:vc animated:YES];
};
[self.view addSubview:_messageView];

}

– (void)viewWillAppear:(BOOL)animated
{
[_messageView startTimer];
}

– (void)viewWillDisappear:(BOOL)animated{
[_messageView stopTimer];
}

demo地址:https://github.com/huanghaiyan/HYScrollMessage-master

分享到: 更多
Separator image Posted in IOS.

发表评论

电子邮件地址不会被公开。 必填项已用*标注