弹幕项目实现

1.代理类的实现文件

#import "AppDelegate.h"

#import"MainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//程序一旦启动会调用的方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //获取当前设备的屏幕的宽和高

    CGRect rect = [[UIScreen mainScreen] bounds];

    //主窗口的创建过程 主窗口有且仅有一个 UIView:视图类

    self.window=[[UIWindow alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];

    //self.window.backgroundColor=[UIColor blackColor];

    self.window.backgroundColor=[UIColor colorWithRed:255/255.0 green:0.0 blue:0.0 alpha:1.0];

    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 44)];//实例化

    label.text=@"用户名";//设置标签的文本

    label.textAlignment=NSTextAlignmentCenter;//设置文本的显示方式(居中,左对齐,右对齐)

    label.font=[UIFont systemFontOfSize:18];//设置标签文本的字体大小

    label.textColor=[UIColor greenColor];//设置文本的颜色

    [self.window addSubview:label];//添加子视图

    

    //UIViewController视图控制器类

    //UIViewController* ctr=[[UIViewController alloc] init];

    //[UIColor clearColor]; //透明色

    MainViewController* ma=[[MainViewController alloc] init];

    

    [self.window setRootViewController:ma];//视图控制器类:分层管理视图

    [self.window makeKeyAndVisible];//让主窗口可见

    return YES;

}

//程序即将取消活跃状态会回调的方法

- (void)applicationWillResignActive:(UIApplication *)application {

}

//程序确实进入后台会回调的方法

- (void)applicationDidEnterBackground:(UIApplication *)application {

  

}

//程序即将进入前台会回调的方法

- (void)applicationWillEnterForeground:(UIApplication *)application {

}

//程序为活跃状态会回调的方法

- (void)applicationDidBecomeActive:(UIApplication *)application {

}

//如果设备不支持后台运行,点击home键 程序退出, 此方法会被回调

//程序不支持后台运行设置:FirstApp-Info.plist文件->增加键值对Application does not run in background=YES 即可。

- (void)applicationWillTerminate:(UIApplication *)application {

  

}

@end

2.控制器类的实现文件

#import "MainViewController.h"

#import"UIColor+Random.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //设置当前类的属性View背景色

    self.view.backgroundColor=[UIColor blackColor];

    //获取plist文件中的内容

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

    self.array=[NSArray arrayWithContentsOfFile:path];

    //用指定路径文件内容初始化数组

    //计时器

    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(initDate) userInfo:nil repeats:YES];

    //参数一:时间间隔  参数二:目标  参数三:指定方法  参数四:是否重复执行指定的方法

    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//创建标签并添加到视图中

-(void)initDate

{

    UILabel* label=[[UILabel alloc] init];

    [label setFrame:CGRectMake(300, arc4random_uniform(568), 360, 44)];

    label.textAlignment=NSTextAlignmentLeft;

    label.text = [self.array objectAtIndex:arc4random_uniform(11)];

    //从数组总随机获取字符串作为标签的文本

    label.textColor = [UIColor randomColor];

    label.font = [UIFont systemFontOfSize:14];

    [self.view addSubview:label];

    [self move:label];

    

}

//移动标签

-(void)move:(UILabel*)_label

{

    [UIView animateWithDuration:5.0 animations:^(){

        [_label setFrame:CGRectMake(-180, _label.frame.origin.y, _label.frame.size.width, _label.frame.size.height)];

    

    } completion:^(BOOL finished) {

        [_label removeFromSuperview];

    }];//参数一:动画的持续时间, 参数二:指定动画  参数三:动画结束后所作的操作。

    

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

}

*/

@end

3.效果:

原文地址:https://www.cnblogs.com/about-zj-blog/p/5334750.html