跑马灯的粗略实现

#import <UIKit/UIKit.h>

@interface VUIAnnouncementView : UIView

//更新公告文案
- (void)unpdataAnnouncementContent:(NSString *)content;

- (id)initWithFrame:(CGRect)frame content:(NSString*)content;

@end
#import "VUIAnnouncementView.h"
@interface VUIAnnouncementView()
@property (nonatomic,strong) UILabel *contentLabel; ///<公告内容Lab
@property (nonatomic,strong) UIButton *closeBtn; ///<关闭按钮
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UIView *leftView;
@property (nonatomic,strong) UIView *rightView;
@property (nonatomic,copy) NSString *content; ///<公告内容
@end

@implementation VUIAnnouncementView

- (id)initWithFrame:(CGRect)frame content:(NSString*)content
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.content = content;
        [self configUI];
        [self configFrame];
        [self startAnimation];
    }
    return self;
}

- (void)configUI {
    self.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    [self addSubview:self.contentLabel];
    [self addSubview:self.leftView];
    [self addSubview:self.imageView];
    [self addSubview:self.rightView];
    [self addSubview:self.closeBtn];
}

- (void)configFrame {
    self.contentLabel.centerY = self.height/2;
    self.contentLabel.left =  self.width;
    self.imageView.frame = CGRectMake(10, (self.height - 24)/2.0, 24, 24);
    self.leftView.frame = CGRectMake(0, 0, self.imageView.right, self.height);
    self.closeBtn.frame = CGRectMake(self.width - 30 ,(self.height - 30)/2.0, 30, 30);
    self.rightView.frame = self.closeBtn.frame;
}

/*动画代码*/
- (void)startAnimation{
    float t = 5;
    float len = self.contentLabel.width + self.width;
    CGAffineTransform endpos = CGAffineTransformMakeTranslation(-len, 0);
    [UIView animateWithDuration:(t * len/self.width) delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.contentLabel.transform = endpos;
    } completion:^(BOOL finished) {
        if (finished) {//必须写上,@selector(endAnimation)中的方法才有用
            self.contentLabel.transform = CGAffineTransformIdentity;
            [self startAnimation];
        }
    }];
}

- (void)endAnimation{
    self.contentLabel.transform = CGAffineTransformIdentity;
    [self.contentLabel.layer removeAllAnimations];//会结束动画,使finished变量返回Null
}

//更新公告文案
- (void)unpdataAnnouncementContent:(NSString *)content
{
    _content = content;
    self.hidden = NO;
    if(content.length > 0)
    {
        [self endAnimation];
        self.contentLabel.text = [NSString stringWithFormat:@"公告: %@",self.content];
        [self.contentLabel sizeToFit];
        self.contentLabel.centerY = self.height/2;
        self.contentLabel.left =  self.width;
        [self startAnimation];
    }
}

- (void)hideView
{
    self.hidden = YES;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel)
    {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.text = [NSString stringWithFormat:@"公告: %@",self.content];
        [_contentLabel setFont:[UIFont systemFontOfSize:14]];
        _contentLabel.textColor = [UIColor colorWithHex:@"909090"];
//        _contentLabel.backgroundColor = [UIColor cyanColor];
        [_contentLabel sizeToFit];
    }
    return _contentLabel;
}

- (UIImageView *)imageView
{
    if (!_imageView)
    {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"公告.tiff"]];
//        _imageView.backgroundColor = [UIColor yellowColor];
    }
    return _imageView;
}

- (UIView *)leftView
{
    if (!_leftView)
    {
        _leftView = [[UIView alloc] init];
        _leftView.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    }
    return _leftView;
}

- (UIView *)rightView
{
    if (!_rightView)
    {
        _rightView = [[UIView alloc] init];
        _rightView.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    }
    return _rightView;
}

- (UIButton *)closeBtn
{
    if (!_closeBtn)
    {
        _closeBtn = [[UIButton alloc]init];
        [_closeBtn setImage:[UIImage imageNamed:@"关闭.tiff"] forState:UIControlStateNormal];
        _closeBtn.layer.cornerRadius = 15;
        _closeBtn.clipsToBounds = YES;
        _closeBtn.backgroundColor = [UIColor redColor];
        [_closeBtn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
@end
发布了131 篇原创文章 · 获赞 9 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Morris_/article/details/103290776