ios实用开发技巧

iOS 跑马灯Label

2017-08-23  本文已影响3989人  Jixin

一、跑马灯Label的设计

二、成果展示

Github 传送门

ZScollLabel.gif

三、接入使用

1.接入

直接将我的项目中中的ZScrollLabel文件夹中的内容添加到工程中即可。

2.使用

- (ZScrollLabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[ZScrollLabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 120) / 2.0, 80, 120, 20)];
        _contentLabel.textColor = [UIColor redColor];
        _contentLabel.font = [UIFont systemFontOfSize:18.0f];
        _contentLabel.scrollDuration = 13;
        _contentLabel.layer.borderWidth = 0.5;
        _contentLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _contentLabel.layer.cornerRadius = 3.0;
    }
    return _contentLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.contentLabel.text = @"一身诗意千寻瀑,万古人间四月天";
    [self.view addSubview:self.contentLabel];
}


3.注意事项

如果使用ZScollLabel的ViewControler有NavigationController,那么请务必要在-(void)viewDidLoad方法中添加self.automaticallyAdjustsScrollViewInsets = NO;让ZScrollLabel的Frame计算从屏幕顶部开始,而不是从导航栏的底部开始。

四、实现原理

滚动动画的代码

[UIView beginAnimations:@"scrollLabel" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationDuration:self.scrollDuration];
    
if (self.label.frame.size.width > self.frame.size.width) {
    self.contentView.frame = CGRectMake(-(self.label.frame.size.width + _paddingBetweenLabels), 0, size.width, size.height);
} else {
    self.contentView.frame = CGRectMake(0, 0, size.width, size.height);
}
    
[UIView commitAnimations];
上一篇 下一篇

猜你喜欢

热点阅读