UI定制 - 跑马灯(文本)
跑马灯
1 - 代码封装
// - JTSlideShadowAnimation.h
1 #import <UIKit/UIKit.h> 2 @interface JTSlideShadowAnimation : NSObject <CAAnimationDelegate> 3 4 @property (strong, nonatomic) UIView *animatedView; 5 @property (strong, nonatomic) UIColor *shadowBackgroundColor; 6 @property (strong, nonatomic) UIColor *shadowForegroundColor; 7 @property (assign, nonatomic) CGFloat shadowWidth; 8 @property (assign, nonatomic) CGFloat repeatCount; 9 @property (assign, nonatomic) NSTimeInterval duration; 10 11 - (void)start;// 开启 12 - (void)stop; // 停止 13 14 @end
// - JTSlideShadowAnimation.m
1 #import "JTSlideShadowAnimation.h" 2 3 @interface JTSlideShadowAnimation(){ 4 CABasicAnimation *currentAnimation; 5 } 6 7 @end 8 9 @implementation JTSlideShadowAnimation 10 11 - (id)init{ 12 self = [super init]; 13 if(!self){ 14 return nil; 15 } 16 17 [self commonInit]; 18 return self; 19 } 20 21 - (void)commonInit{ 22 23 self.shadowBackgroundColor = [UIColor colorWithWhite:1 alpha:.6]; 24 self.shadowForegroundColor = [UIColor whiteColor]; 25 self.shadowWidth = 30; 26 self.repeatCount = HUGE_VALF; 27 self.duration = 2.0; 28 } 29 30 - (void)start{ 31 if(!self.animatedView){ 32 NSLog(@"JTSlideShadowAnimation no view to animate"); 33 return; 34 } 35 [self stop]; 36 37 CAGradientLayer *gradientMask = [CAGradientLayer layer]; 38 gradientMask.frame = self.animatedView.bounds; 39 CGFloat gradientSize = self.shadowWidth / self.animatedView.frame.size.width; 40 41 NSArray *startLocations = @[@0,[NSNumber numberWithFloat:(gradientSize / 2.)],[NSNumber numberWithFloat:gradientSize]]; 42 NSArray *endLocations = @[[NSNumber numberWithFloat:(1. - gradientSize)],[NSNumber numberWithFloat:(1. - (gradientSize / 2.))],@1]; 43 44 gradientMask.colors = @[(id)self.shadowBackgroundColor.CGColor,(id)self.shadowForegroundColor.CGColor,(id)self.shadowBackgroundColor.CGColor]; 45 46 gradientMask.locations = startLocations; 47 gradientMask.startPoint = CGPointMake(0 - (gradientSize * 2), .5); 48 gradientMask.endPoint = CGPointMake(1 + gradientSize, .5); 49 50 self.animatedView.layer.mask = gradientMask; 51 currentAnimation = [CABasicAnimation animationWithKeyPath:@"locations"]; 52 currentAnimation.fromValue = startLocations; 53 currentAnimation.toValue = endLocations; 54 currentAnimation.repeatCount = self.repeatCount; 55 currentAnimation.duration = self.duration; 56 currentAnimation.delegate = self; 57 58 [gradientMask addAnimation:currentAnimation forKey:@"JTSlideShadowAnimation"]; 59 } 60 61 - (void)stop{ 62 63 if(self.animatedView && self.animatedView.layer.mask){ 64 [self.animatedView.layer.mask removeAnimationForKey:@"JTSlideShadowAnimation"]; 65 self.animatedView.layer.mask = nil; 66 currentAnimation = nil; 67 } 68 } 69 70 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)finished{ 71 if(anim == currentAnimation){ 72 [self stop]; 73 } 74 } 75 76 @end
2 - 代码示例:如何使用
// - ViewController.m
1 #import "ViewController.h" 2 #import "JTSlideShadowAnimation.h" 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 self.view.backgroundColor = [UIColor whiteColor]; 12 13 UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 230, 280, 50)]; 14 testLabel.text = @"hello,look at me !"; 15 [self.view addSubview:testLabel]; 16 17 JTSlideShadowAnimation *testJT = [[JTSlideShadowAnimation alloc] init]; 18 testJT.animatedView = testLabel; 19 testJT.shadowBackgroundColor = [UIColor colorWithWhite:1 alpha:.5]; 20 testJT.shadowForegroundColor = [UIColor redColor]; 21 testJT.duration = 1.0f;// 时间 22 [testJT start];// 开启动画 23 } 24 25 26 @end
运行效果
分类:
iOS功能封装
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)