@class LGJAutoRunLabel;
typedef NS_ENUM(NSInteger, RunDirectionType) {
LeftType = 0,
RightType = 1,
};
@protocol LGJAutoRunLabelDelegate <NSObject>
@optional
- (void)operateLabel: (LGJAutoRunLabel *)autoLabel animationDidStopFinished: (BOOL)finished;
@end
@interface LGJAutoRunLabel : UIView
@property (nonatomic, weak) id <LGJAutoRunLabelDelegate> delegate;
@property (nonatomic, assign) CGFloat speed;
@property (nonatomic, assign) RunDirectionType directionType;
- (void)addContentView: (UIView *)view;
- (void)startAnimation;
- (void)stopAnimation;
@interface LGJAutoRunLabel()<CAAnimationDelegate>
{
CGFloat _width;
CGFloat _height;
CGFloat _animationViewWidth;
CGFloat _animationViewHeight;
BOOL _stoped;
UIView *_contentView;//滚动内容视图
}
@property (nonatomic, strong) UIView *animationView;//放置滚动内容视图
@end
- (instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
_width = frame.size.width;
_height = frame.size.height;
self.speed = 1.0f;
self.directionType = LeftType;
self.layer.masksToBounds = YES;
self.animationView = [[UIView alloc] initWithFrame:CGRectMake(_width, 0, _width, _height)];
[self addSubview:self.animationView];
}
return self;
}
- (void)addContentView:(UIView *)view {
[_contentView removeFromSuperview];
view.frame = view.bounds;
_contentView = view;
self.animationView.frame = view.bounds;
[self.animationView addSubview:_contentView];
_animationViewWidth = self.animationView.frame.size.width;
_animationViewHeight = self.animationView.frame.size.height;
}
- (void)startAnimation {
[self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
_stoped = NO;
CGPoint pointRightCenter = CGPointMake(_width + _animationViewWidth / 2.f, _animationViewHeight / 2.f);
CGPoint pointLeftCenter = CGPointMake(-_animationViewWidth / 2, _animationViewHeight / 2.f);
CGPoint fromPoint = self.directionType == LeftType ? pointRightCenter : pointLeftCenter;
CGPoint toPoint = self.directionType == LeftType ? pointLeftCenter : pointRightCenter;
self.animationView.center = fromPoint;
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];
[movePath addLineToPoint:toPoint];
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.path = movePath.CGPath;
moveAnimation.removedOnCompletion = YES;
moveAnimation.duration = _animationViewWidth / 30.f * (1 / self.speed);
moveAnimation.delegate = self;
[self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];
}
- (void)stopAnimation {
_stoped = YES;
[self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (self.delegate && [self.delegate respondsToSelector:@selector(operateLabel:animationDidStopFinished:)]) {
[self.delegate operateLabel:self animationDidStopFinished:flag];
}
if (flag && !_stoped) {
[self startAnimation];
}
}
//MARK:- CreateAutoRunLabel
- (void)createAutoRunLabel {
LGJAutoRunLabel *runLabel = [[LGJAutoRunLabel alloc] initWithFrame:CGRectMake(0, 100, kWidth, 50)];
runLabel.delegate = self;
runLabel.directionType = LeftType;
[self.view addSubview:runLabel];
[runLabel addContentView:[self createLabelWithText:@"繁华声 遁入空门 折煞了梦偏冷 辗转一生 情债又几 如你默认 生死枯等 枯等一圈 又一圈的 浮图塔 断了几层 断了谁的痛直奔 一盏残灯 倾塌的山门 容我再等 历史转身 等酒香醇 等你弹 一曲古筝" textColor:[selfrandomColor] labelFont:[UIFont systemFontOfSize:14]]];
[runLabel startAnimation];
}
- (UILabel *)createLabelWithText: (NSString *)text textColor:(UIColor *)textColor labelFont:(UIFont *)font {
NSString *string = [NSString stringWithFormat:@"%@", text];
CGFloat width = [UILabel getWidthByTitle:string font:font];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 50)];
label.font = font;
label.text = string;
label.textColor = textColor;
return label;
}
- (UIColor *)randomColor {
return [UIColor colorWithRed:[self randomValue] green:[self randomValue] blue:[self randomValue] alpha:1];
}
- (CGFloat)randomValue {
return arc4random()%255 / 255.0f;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有