简单的实现弹幕效果

用键盘输入,按下回车键

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textInPut;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置文本框的代理
    self.textInPut.delegate = self;

}

//点击return键调用的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    UILabel *textLabel = [[UILabel alloc]init];
    [self.view addSubview:textLabel];
    
    textLabel.text = self.textInPut.text;
    
    //清空输入框
    self.textInPut.text = nil;
    
    //设置label的大小
    [textLabel sizeToFit];
    
    CGFloat w = textLabel.bounds.size.width;
    CGFloat h = textLabel.bounds.size.height;
    CGFloat x = [UIScreen mainScreen].bounds.size.width;
    CGFloat y = arc4random_uniform([UIScreen mainScreen].bounds.size.height -h);
    
    textLabel.frame = CGRectMake(x, y, w, h);
    
    //动画
    [UIView animateWithDuration:5.0 animations:^{
        textLabel.frame = CGRectMake(-w, y, w, h);
    }];
    
    return YES;
}

@end

 

posted @ 2016-05-18 15:09  小眼奇遇记  阅读(276)  评论(0编辑  收藏  举报