博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS之跑马灯的实现
阅读量:4289 次
发布时间:2019-05-27

本文共 5397 字,大约阅读时间需要 17 分钟。

#import "LYAnimatePaomadengVC.h"

 

@interface LYAnimatePaomadengVC ()

 

@end

 

@implementation LYAnimatePaomadengVC

 

- (void)viewDidLoad {

    [superviewDidLoad];

   

 

 

}

 

//UIView动画实现跑马灯

-(void)UIVIEwAnomatePaomadeng{

    UILabel *label3 = [[UILabelalloc] initWithFrame:CGRectMake(10,200,self.view.bounds.size.width*4,100)];

    label3.backgroundColor = [UIColorredColor];

    label3.text =@"噜啦啦噜啦啦噜啦噜啦噜,噜啦噜啦噜啦噜啦噜啦噜~~~lllllllllllllllllllllllllllllllllllllllllll噜啦啦噜啦啦噜啦噜啦噜,噜啦噜啦噜啦噜啦噜啦噜~~~lllllllllllllllllllllllllllllllllllllllllll";

    [self.viewaddSubview:label3];

    

    CGRect frame = label3.frame;

    frame.origin.x =1000;

    label3.frame = frame;

    [UIViewbeginAnimations:@"testAnimation"context:NULL];

    [UIViewsetAnimationDuration:10.0f];

    [UIViewsetAnimationCurve:UIViewAnimationCurveLinear];

    [UIViewsetAnimationDelegate:self];

    [UIViewsetAnimationRepeatAutoreverses:NO];

    [UIViewsetAnimationRepeatCount:115];

    frame = label3.frame;

    frame.origin.x =-350;

    label3.frame = frame;

    [UIViewcommitAnimations];

}

 

//UIVIew实现跑马灯方式二

-(void)paomadeng{

    UILabel *aUILabel=[[UILabelalloc]initWithFrame:CGRectMake(0,60, 500, 50)];

    aUILabel.text=@"asafdsfdafdsafdsafdafdsafdsfdsafdsafdafsfafdfdsafdafdsfdafdsadsa";

    aUILabel.textColor=[UIColorredColor];

    [self.viewaddSubview:aUILabel];

    //取消所有的动画

        //[self.aUILabel.layer removeAllAnimations];

    //计算实际text大小

    CGSize  textSize =  [aUILabel.text sizeWithAttributes:@{

NSFontAttributeName: [UIFontsystemFontOfSize:15]}];

    //保存label的frame

    CGRect lframe =aUILabel.frame;

    //用计算出来的text的width更改frame的原始width

    lframe.size.width= textSize.width;//从屏幕最右边向左边移

    lframe.origin.x=self.view.bounds.size.width; //用新值更改label的原frame值

    aUILabel.frame= lframe;

    //计算动画x移动的最大偏移:屏幕width+text的width

    float offset = textSize.width+self.view.bounds.size.width;

    [UIViewanimateWithDuration:10.0delay:0options:UIViewAnimationOptionRepeat//动画重复的主开关

     |UIViewAnimationOptionCurveLinear//动画的时间曲

                     animations:^{aUILabel.transform=CGAffineTransformMakeTranslation(-offset,0);} completion:^(BOOL finished) {

                }

            ];

    

}

 

@end

 

==============scrollView实现跑马灯

 

#import "LYScrollPaomadengVC.h"

#define WIDTH     [UIScreen mainScreen].bounds.size.width

#define HEIGHT     [UIScreen mainScreen].bounds.size.height

#define ADHEIGHT 30

@interface LYScrollPaomadengVC ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIView *infoscrollview;//盛放轮播infolabel的View

@property(nonatomic,strong)UILabel *scrollLabel;//轮播消息

@property(nonatomic,strong)NSTimer *timer;//定时器

@property(nonatomic,strong)UIScrollView *infoScroll;//消息轮播

@property(nonatomic,assign)CGFloat scrolllabelWidth;//滚动消息的宽度

@property(nonatomic,assign)CGFloat infoscrollAheadlength;//前进的距离

@end

 

@implementation LYScrollPaomadengVC

 

- (void)viewDidLoad {

    [superviewDidLoad];

   

    //轮播小广播

    UIView *infoscrollview=[[UIViewalloc]initWithFrame:CGRectMake(0,ADHEIGHT, WIDTH,40+2)];

    [self.viewaddSubview:infoscrollview];

    self.infoscrollview=infoscrollview;

    infoscrollview.backgroundColor=[UIColorwhiteColor];

    UIImageView *imageV=[[UIImageViewalloc]initWithFrame:CGRectMake(15,10, 20, 20)];

    imageV.image=[UIImageimageNamed:@"homeScrollInfo"];

    [infoscrollview addSubview:imageV];

    

    if(!self.infoScroll){

        self.infoScroll=[[UIScrollViewalloc]initWithFrame:CGRectMake(50,0,WIDTH-50,40)];

    }

    self.infoScroll.scrollEnabled=NO;

    self.infoScroll.delegate=self;

    self.infoScroll.showsHorizontalScrollIndicator=NO;

    [self.infoscrollviewaddSubview:self.infoScroll];

//    for (UIView *v in self.infoScroll.subviews) {

//        [v removeFromSuperview];

//    }

    self.scrollLabel=[[UILabelalloc]init];

    self.scrollLabel.text=@"2号钱包,取现0.35%费率,秒到";

    self.scrollLabel.textColor=[UIColorgrayColor];

    self.scrollLabel.font=[UIFontsystemFontOfSize:15];

    // 给 显示的文本一个区域

    CGSize contentMaxSizes = CGSizeMake(MAXFLOAT,40);

    // NSFontAttributeName 字体的大小

    NSDictionary *attributesDicts = @{

NSFontAttributeName:[UIFontsystemFontOfSize:15]};

    //计算文本实际宽高的时候, 计算的字体大小要和label中设置的字体大小保持一致

    // 根据限定的条件, 来计算text 真实的宽高

    CGSize contentRealSizes =  [self.scrollLabel.textboundingRectWithSize:contentMaxSizes options:NSStringDrawingUsesLineFragmentOriginattributes:attributesDicts context:nil].size;

    self.scrolllabelWidth=contentRealSizes.width;//滚动消息的实际宽度

    self.scrollLabel.frame=CGRectMake(0,0, contentRealSizes.width,40);/

    self.infoScroll.contentSize=CGSizeMake(contentRealSizes.width+1,0);

    [self.infoScrolladdSubview:self.scrollLabel];

    

}

 

//在将要push出去的时候把导航栏清空,显示下面的横线

-(void)viewWillDisappear:(BOOL)animated{

    [superviewWillDisappear:animated];

   

    

    //    // 让计时器无效

    

    if ([self.timerisValid]) {

        [self.timerinvalidate];

        self.timer =nil;

    }

    //关闭定时器

    //    [self.timer setFireDate:[NSDate distantFuture]];

}

 

 

-(void)viewDidAppear:(BOOL)animated{

 

    [selfsettimer];//infolunbo

}

-(void)infolunbo{

    NSLog(@"定时器执行");

    if(self.infoscrollAheadlength<=self.scrolllabelWidth-2){

        self.infoscrollAheadlength+=2;

        [self.infoScrollsetContentOffset:CGPointMake(self.infoscrollAheadlength,0)];

    }else{

//回到起点

        self.infoscrollAheadlength=0.0;

        [self.infoScrollsetContentOffset:CGPointMake(0,0)];

    }

}

 

//定时器调用

-(void)settimer{

    NSLog(@"定时器创建");

    NSTimer *timer = [NSTimertimerWithTimeInterval:0.1target:selfselector:@selector(infolunbo)userInfo:nilrepeats:YES];

    [[NSRunLoopmainRunLoop] addTimer:timerforMode:NSDefaultRunLoopMode];

    self.timer=timer;

}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    [self.timerinvalidate];//定时器失效

    self.timer=nil;

    self.infoscrollAheadlength=0;

    [self.infoScrollsetContentOffset:CGPointMake(0,0)];

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [selfsettimer];//开启定时器

}

@end

 

框架:

swift:

转载地址:http://gjmgi.baihongyu.com/

你可能感兴趣的文章
有序数组求交集
查看>>
文字常量区与栈
查看>>
非阻塞connect 编写方法
查看>>
epoll 边沿触发
查看>>
String类 默认生成的函数
查看>>
Linux 软连接与硬链接
查看>>
视音频数据处理入门:H.264视频码流解析
查看>>
视音频数据处理入门:AAC音频码流解析
查看>>
视音频数据处理入门:UDP-RTP协议解析
查看>>
视音频数据处理入门:FLV封装格式解析
查看>>
最简单的基于FFMPEG的封装格式转换器(无编解码)
查看>>
base64 编码原理
查看>>
单链表是否有环的问题
查看>>
判断两个链表是否相交并找出交点
查看>>
归并排序
查看>>
STL常见问题
查看>>
time_wait和close_wait状态
查看>>
STL中vector、list、deque和map的区别
查看>>
Linux下多线程查看工具(pstree、ps、pstack)
查看>>
PID PPID LWP NLWP
查看>>