iOS动画iOS devJC专题

ios--简单弹幕实现

2016-05-17  本文已影响1155人  AlexPei
02.gif

#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScreenB [UIScreen mainScreen].bounds
@interface ViewController ()<UITextFieldDelegate>

//输入框
@property (weak, nonatomic) IBOutlet UITextField *TXTInput;

//判断是否买了会员
@property (nonatomic, assign) BOOL isVip;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    /** 设置代理,监听键盘return键 */
    self.TXTInput.delegate = self;

}


/** 键盘回车return键调用的方法 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    
   UILabel * lbl = [[UILabel alloc] init];
    [self.view addSubview:lbl];
    
    /** 判断是否是Vip */
//根据是否是Vip来设置label的背景色
    if (self.isVip) {
        
        lbl.backgroundColor = [UIColor redColor];
    } else {
    
        lbl.backgroundColor = [UIColor whiteColor];
    }
    
    lbl.text = self.TXTInput.text;
    
    /** 每次点击return键后清空输入框 */
    self.TXTInput.text = nil;
    
    /** 设置lbl自动适应文字大小 */
    [lbl sizeToFit];
    
    CGFloat w = self.lbl.bounds.size.width;
    CGFloat h = self.lbl.bounds.size.height;
    CGFloat x = kScreenW;

//随机产生y的值,来随机产生lbl的高度
    CGFloat y = arc4random_uniform(kScreenH-self.lbl.bounds.size.height);
    
    lbl.frame = CGRectMake(x, y, w, h);
    
    /** 动画执行 */
    [UIView animateWithDuration:5.0 animations:^{
       
        lbl.frame = CGRectMake(-self.lbl.bounds.size.width, y, w, h);
    }];

    return YES;
}

- (IBAction)clickVipBtn:(UIButton *)sender {
    
    if (self.isVip) {
        self.isVip = NO;
        [sender setTitle:@"买会员" forState:UIControlStateNormal];
        
        /** 存储偏好设置或者服务器 */
        
    } else {
        [sender setTitle:@"去会员" forState:UIControlStateNormal];
        self.isVip = YES;
        /** 存储偏好设置或者服务器 */
    }
}

思路:通过产生随机数产生lbl的不同高度,动画实现通过uiview的动画.

上一篇下一篇

猜你喜欢

热点阅读