- 无代码不博客.实现太简单不做过多介绍,边看代码变解释.
- 先看效果图片
#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
- 设置TXTInput的代理,来监听键盘上return建是否被点击.
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/** 设置代理,监听键盘return键 */
self.TXTInput.delegate = self;
}
- 下面是点击return建调用的方法
/** 键盘回车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的动画.
网友评论