由于业务需求,需要实现一个垂直滚动文本的跑马灯,没有办法只有自己去写一个动画效果,下面直接贴出代码。
MTAVerticalLoopView.h文件的代码如下:
//
// UIVerticalLoopView.h
// Jovi
//
// Created by yuzhuo on 2016/11/23.
// Copyright © 2016年 dianping.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MTANewAdviseInfo.h"
@protocol VerticalLoopDelegate<NSObject>
- (void)didClickContentAtIndex:(NSInteger)index;
@end
typedef enum
{
VerticalLoopDirectionBottom,
VerticalLoopDirectionDown,
}VerticalLoopDirection;
@interface MTAVerticalLoopView : UIView
{
// 创建两个label循环滚动
UILabel *_firstContentLabel;
UILabel *_secondContentLabel;
UILabel *_firstclickLabel;
UILabel *_secondclickLabel;
// 记录
int currentIndex;
}
/** 动画方向默认往上
* 跑马灯动画时间
*/
@property(nonatomic) float verticalLoopAnimationDuration;
/**
* 显示的内容(支持多条数据)
*/
@property(nonatomic, retain) NSMutableArray<MTANewAdviseInfo *> *data;
/**
* loop方向(上下/右)
*/
@property(nonatomic) VerticalLoopDirection Direction;
@property (nonatomic, weak)id<VerticalLoopDelegate> loopDelegate;
/**
* 开启
*/
-(void)start;
- (void)loopContentClick;
@end
MTAVerticalLoopView.m文件的代码如下:
//
// UIVerticalLoopView.m
// Jovi
//
// Created by yuzhuo on 2016/11/23.
// Copyright © 2016年 dianping.com. All rights reserved.
//
#import "MTAVerticalLoopView.h"
@implementation MTAVerticalLoopView
{
BOOL _animating;
}
#pragma mark - lifecycle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupView];
}
return self;
}
#pragma mark - private method
-(void)setupView {
_firstclickLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 10, 50, 25)];
_firstclickLabel.font = [UIFont systemFontOfSize:11.0];
[_firstclickLabel.layer setBorderColor:[UIColor colorWithHexString:@"#ff6633"].CGColor];//边框颜色
[_firstclickLabel.layer setMasksToBounds:YES];
[_firstclickLabel.layer setCornerRadius:12.5];
[_firstclickLabel.layer setBorderWidth:1.0];
_firstclickLabel.textColor = [UIColor colorWithHexString:@"#ff6633"];
_firstclickLabel.textAlignment = NSTextAlignmentCenter;
_secondclickLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-50, self.frame.size.height+10, 50, 25)];
_secondclickLabel.font = [UIFont systemFontOfSize:11.0];
[_secondclickLabel.layer setBorderColor:[UIColor colorWithHexString:@"#ff6633"].CGColor];//边框颜色
[_secondclickLabel.layer setMasksToBounds:YES];
[_secondclickLabel.l