posts - 186,  comments - 17,  views - 35万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

通常显示不够了,比如八个字。只能显示6个字 。产品要求 来回滚动 

下面有两种 方法 : 一种UIScrollView  一种 View动画  如果不能满足你  请点击这个  https://github.com/cbpowell/MarqueeLabel  好用的第三方

- (void)viewDidLoad
{
	[super viewDidLoad];
	self.view.backgroundColor = [UIColor blackColor];

	// 注册字体
	REGISTER_FONT(bundleFont(@"新蒂小丸子体.ttf"), @"新蒂小丸子体");
	
	// 获取文本
	NSString *string = @"  喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。《莲的心事》,你似琉璃一样的晶莹,柔柔地拨动我多情的心弦。我,莲的心事,有谁知?我,莲的矜持,又有谁懂?  ";
	
	// 初始化label
	UILabel *label	  = [UILabel new];
	label.text		  = string;
	label.numberOfLines = 0;
	label.textColor	 = [UIColor cyanColor];
	label.font		  = [UIFont fontWithName:CUSTOM_FONT(@"新蒂小丸子体", 0)
										  size:20.f];
	
	// 计算尺寸
	CGSize size		 = [label boundingRectWithSize:CGSizeMake(0, 0)];
	label.frame		 = (CGRect){CGPointZero, size};

	// 初始化ScrollView
	UIScrollView *showView = \
		[[UIScrollView alloc] initWithFrame:CGRectMake(0, 90, 320, size.height)];
	showView.contentSize   = size;
	showView.showsHorizontalScrollIndicator = NO;
	[showView addSubview:label];
	[self.view addSubview:showView];
	
	// 形成边缘的遮罩
	UIImageView *imageView = \
	[[UIImageView alloc] initWithFrame:CGRectMake(0, 90, 320, size.height)];
	imageView.image = [UIImage imageNamed:@"bg"];
	[self.view addSubview:imageView];
	
	// 动画
	[UIView animateKeyframesWithDuration:10
								   delay:7
								 options:UIViewKeyframeAnimationOptionAllowUserInteraction
							  animations:^{
								  // 计算移动的距离
								  CGPoint point = showView.contentOffset;
								  point.x = size.width - 320.f;
								  showView.contentOffset = point;
							  }
							  completion:^(BOOL finished) {
								  
							  }];
}

第二种 简洁版
#import "Ridehorselight.h"

@interface Ridehorselight ()

@property (nonatomic, strong) NSTimer *timer; //定时器
@property (nonatomic, weak) UIView *viewAnima; //Label的背景
@property (nonatomic, weak) UILabel *customLabel; //Label

@end

@implementation Ridehorselight

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self addClildView];
    [self setTimer];
}

- (void)addClildView {

    self.view.backgroundColor = [UIColor whiteColor];

    CGFloat viewX = (self.view.frame.size.width-200)/2;
    UIView *viewAnima = [[UIView alloc] initWithFrame:CGRectMake(viewX, 100, 200, 40)];
    viewAnima.backgroundColor = [UIColor  yellowColor];
    self.viewAnima = viewAnima;
    self.viewAnima.clipsToBounds = YES; //剪掉超出view范围部分

    CGFloat customLabelY = (self.viewAnima.frame.size.height-30)/2;
    UILabel *customLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.viewAnima.frame.size.width, customLabelY, 200, 30)];
    customLabel.text = @"跑马灯效果,滚动文本!";
    customLabel.textColor = [UIColor redColor];
    self.customLabel = customLabel;

    [self.view addSubview:viewAnima];
    [viewAnima addSubview:customLabel];
}

- (void)setTimer {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changePos) userInfo:nil repeats:YES];
}

- (void)changePos {

    CGPoint cenPos = self.customLabel.center;
    if (cenPos.x < -100) {

        CGFloat distance = self.customLabel.frame.size.width/2;
        self.customLabel.center = CGPointMake(self.viewAnima.frame.size.width+distance, 20);
    } else {
        self.customLabel.center = CGPointMake(cenPos.x-5, 20);
    }
}

 

 

 

posted on   ZOYOO  阅读(553)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示