1,文本框 UITextField
UILabel可以在界面中显示文本,但是用户无法选择或者编辑,相反,UITextField可以接受用户输入的文本,例如在登录界面中,如果需要用户输入用户名和密码,就可以使用UITextField
打开BNRHypnosisViewContro.m 修改loadView方法,向view中添加一个UITextField对象:
- (void)loadView
{
CGRect frame = [UIScreen mainScreen].bounds;
BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] initWithFrame:frame];
CGRect textFieldRect = CGRectMake(40, 70, 240, 30);
UITextField *textField=[[UITextField alloc] initWithFrame:textFieldRect];
textField.borderStyle=UITextBorderStyleRoundedRect;
[backgroundView addSubview:textField];
self.view = backgroundView;
}
构建并运行,比原来多显示了一个文本框,点击文本框会弹出键盘。
设置UITextField的键盘
UITextField对象有一系列属性用来设置弹出的键盘,下面修改2个,添加占位符文本并修改键盘的换行键类型。
textField.placeholder=@"Hypnotize me ";
textField.returnKeyType=UIReturnKeyDone;
看到 return 变成了done
2,委托
像UITextField这类复杂的对象,apple使用委托设计模式。这个对象有一个委托属性,通过为这个对象设置委托,会在发生事件时间向委托发送相应的消息,由委托处理该事件。
例如编辑UITextField对象文本内容的事件,有以下两个对应的委托方法:
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
还有一类带有返回时的委托方法,用于委托中查询需要的信息,例如
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%@",textField.text);
return YES;
}
输入文字,点击done,控制台会输出文字。
3,协议
凡是支持协议的对象,背后都有一个相应的协议(接口),声明可以向该对象的委托对象发送的消息,委托对象根据这个协议为其感兴趣的事件实现相应的方法。如果一个类实现了某个协议中规定的方法,就称这个类遵守该协议。
声明协议的语法是
@protocol开头 +协议名称+<xxx协议包含>
接着声明新协议特有方法
(
分两类 一类@optional 是可选的协议,和必须协议。协议方法默认是必须的,使用@optional可以把后面的方法声明为可选的
)
@end结束
例如
@protocol UITextFieldDelegate<NSObject>
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
@end
要让编译器能够执行检查某个类是否实现了相应协议的必须方法,把相应的类声明为遵守指定的协议,语法格式在头文件或者类扩展的@interface指令末尾 加尖括号里把协议以逗号分隔的列表形式写进去。
例如把BNRHypnosisViewController声明为遵循UITextFieldDelegate协议
代码如下
@interface BNRHypnosisViewController()<UITextFieldDelegate>
@end
4,向屏幕中添加UILabel对象
增强HypnoNerd的催眠效果,本节添加出现在屏幕随机位置的UILabel对象。
首先在BNRHypnosisViewController.m中添加新方法如下
- (void)drawHypnoticMessage:(NSString *)message
{
for(int i = 0; i < 20; i++)
{
UILabel *messageLabel = [[UILabel alloc] init];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor = [UIColor whiteColor];
messageLabel.text = message;
[messageLabel sizeToFit];
//获取随机的X Y座标
int width=(int)(self.view.bounds.size.width - messageLabel.bounds.size.width);
int x = arc4random() % width;
int height=(int)(self.view.bounds.size.height - messageLabel.bounds.size.height);
int y = arc4random() % height;
//把UILabel对象添加到BNR的view中
CGRect frame = messageLabel.frame;
frame.origin = CGPointMake(x, y);
messageLabel.frame = frame;
[self.view addSubview:messageLabel];
}
}
接下来修改textFieldShouldReturn:
点击done之后将要发生的事情:
发送添加对象,重置文本,收齐键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self drawHypnoticMessage:textField.text];
textField.text=@"";
[textField resignFirstResponder];
return YES;
}
构建并运行,输入文字,点击done 输出的文字随机出现在屏幕的各个位置。
增加运动效果
通过UIInterpolatingMotionEffect 类实现效果,设置方向,键路径,相对最大最小值,添加到某个视图上
在BNRHypnosisViewController.m 中,修改drawHypnoticMessage:方法,为UILabel对象分别添加水平方向和垂直方向的视察效果,使UILabel对象的中心点座标在每个方向上最多移动25点。
//添加视觉效果
UIInterpolatingMotionEffect *motionEffecet;
motionEffecet = [[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffecet.minimumRelativeValue = @(-25);
motionEffecet.maximumRelativeValue = @(25);
[messageLabel addMotionEffect:motionEffecet];
motionEffecet = [[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
motionEffecet.minimumRelativeValue = @(-25);
motionEffecet.maximumRelativeValue = @(25);
[messageLabel addMotionEffect:motionEffecet];
在真实设备中运行才可看到效果,倾斜屏幕看到UILabel对象随着倾斜方向移动。