IOS中弹幕的实现

简单的弹幕实现   写的不好的地方请大家指教

在工程设置中要设置横屏的;

实现原理:用定时器来不断产生lable,把lable产生的位置控制在屏幕最右侧 我们看不到的地方 ,我们在写一个移动的方法让产生的lable移动到最左侧不可见的地方,然后remove掉lable,这样就实现了一个简单的弹幕;

 

关于定时器NSTimer

创建  

 NSTimer* mytimer = [NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>];

TimeInterval :执行之前等待的时间 如设置1 则等待1秒执行

target: 发送的对象

selector: 执行的方法  在本例中我们要执行随机创建lable的方法;

userinfo:  此参数可以为nil;

repeats:是否循环,当为YES时循环到实效或释放 此时必须加上

[[NSRunLoop mainRunLoop] addTimer:mytimer forMode:NSDefaultRunLoopMode];

当为NO时只运行一次 会自动把timer加入MainRunloop的NSDefaultRunLoopMode中   为no时 如使用通知时可以使用;

            

代码:

#import "MainViewController.h"

#import "UIColor+Random.h" //UIColor 的分类 实现产生随机颜色   

@interface MainViewController ()

{

    NSTimer* mytimer; 

    NSMutableArray* arrays; //存放要弹出的字符串

    UIButton* start;  //开始按钮

    UIButton* close; //关闭按钮

}

@end

 

@implementation MainViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    arrays = [[NSMutableArray alloc]initWithCapacity:10]; 

//下边是我从别的小代码中找的一个plist文件,从中读取数据 做弹幕  你可以简单的建一个数组加入一些字符串;

    NSString* path = [[NSBundle mainBundle] pathForResource:@"DishData" ofType:@"plist"]; 

    NSArray* array = [[NSArray alloc]initWithContentsOfFile:path];

    for(NSDictionary* arr in array){

        NSString* name = [arr objectForKey:@"name"];

        NSNumber* price = [arr objectForKey:@"price"];

        NSString* str = [NSString stringWithFormat:@"%@",price];

        NSString* description = [arr objectForKey:@"description"];

        [arrays addObject:name];

        [arrays addObject:str];

        [arrays addObject:description]

}

//按钮的一些设定

start = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 100, 50)];

    [start addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];

    [start setTitle:@"开启" forState:UIControlStateNormal];

    [start setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:start];

close = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 100, 50)];

    [close addTarget:self action:@selector(EndTimer:) forControlEvents:UIControlEventTouchUpInside];

    [close setTitle:@"关闭" forState:UIControlStateNormal];

    [close setBackgroundColor:[UIColor greenColor]];

    close.hidden = YES;

    [self.view addSubview:close];

}

-(void)start:(id)sender{

    if(mytimer){

        //开启定时器

        [mytimer setFireDate:[NSDate distantPast]];

    }else{

        mytimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(initLable:) userInfo:nil repeats:YES];

      }

//和关闭按钮中的实现按钮的隐藏和重现

    start.hidden = YES;

    close.hidden = NO;

}

//关闭按钮实现方法

-(void)EndTimer:(id)sender{

    //关闭定时器

    [mytimer setFireDate:[NSDate distantFuture]];

    //取消定时器   删除定时器 这里只说下这个方法,这里用不到

    // [mytimer invalidate];

    close.hidden = YES;

    start.hidden = NO;

}

//创建lable

-(void)initLable:(id)sender{

//lable显示在屏幕右侧  没在屏幕上显示

    UILabel* lable = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.height, rand()%30*10, 100, 20)];

//randomColor是我用分类实现的一个类方法 在文章下边说一下

    [lable setTextColor:[UIColor randomColor]];

    [lable setText:[arrays objectAtIndex:rand()%[arrays count]]];

    [self.view addSubview:lable];

    [self move:lable];

}

 //使lable移动到屏幕左侧  

-(void)move:(UILabel*)lable{

    [UIView animateWithDuration:2 animations:^{

        lable.frame = CGRectMake(0-lable.frame.size.width, lable.frame.origin.y, lable.frame.size.width, lable.frame.size.height);

    }completion:^(BOOL finished) {

        [lable removeFromSuperview];

    }];

}

 //UIColor分类实现产生随机色

//创建分类的方法 :新建Objective-C category;      Category on UIColor;

#import <UIKit/UIKit.h>

@interface UIColor (Random)

//生成随机颜色

+(UIColor*)randomColor;

@end

#import "UIColor+Random.h"

 @implementation UIColor (Random)

+(UIColor*)randomColor{

    static BOOL seed = NO;

    if(!seed){

        seed = YES;

        srandom(time(NULL));

    }

    //三原色可以混合出所有的颜色,同时相加为白色

    CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;

    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;

    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;

    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];//alpha为透明度0完全透明1不透明

    

}

@end

 

posted @ 2015-09-15 19:48  这个西瓜不甜  阅读(328)  评论(0编辑  收藏  举报