继续修改01笔记中的HypnoNerd应用程序,添加供用户输入的Text Field,将用户输入的字符串显示到View中。
1. UITextField
修改BNRHypnosisViewController.m的loadView方法,添加UITextField
CGRect textFieldRect = CGRectMake(40, 70, 240, 30);
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldRect];
// Setting the border style on the text field will allow us to see it more easily
textField.borderStyle = UITextBorderStyleRoundedRect;
[backgroundView addSubview:textField];
2. UIResponder
UIResponder是UIKit框架的一个抽象类,它是UIView、UIViewController和UIApplication类的基类。它定义了用于处理诸如touch事件、手势事件、remote control事件(暂停或播放)。子类重载这些方法来实现对这些事件的处理。
UIWindow.firstResponder指针标示了谁来响应这些事件,当我们选中一个text field时,firstResponder指针就指向这个text field,它就负责响应手势和remote control事件。
要让一个控件成为firstResponder,可以向其发送becomeFirstResponder事件,要隐藏键盘,则发送resignFirstResponder。
3. 配置键盘
修改loadView方法,设置站位字符串和返回键类型
textField.placeholder = @"Hypnotize me";
textField.returnKeyType = UIReturnKeyDone;
4. delegate
在这里代理模式的简单含义就是告诉UITextField,这是它的代理,当某件事情发生时,发送消息通知它的代理。
比如,开始编辑和结束编辑:
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
另外一些是发送消息给代理并进行询问,例如,“我要结束编辑并隐藏键盘了,好吗?”
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
接下来,我们让BNRHypnosisViewController成为text field的代理,并实现textFieldShouldReturn方法,这样当我们点击Done按钮,这个方法会被自动调用。
声明本类服从UITextfieldDelegate协议:
@interface BNRHypnosisViewController () <UITextFieldDelegate>
@end
设置textField的代理指针并实现代理函数:
loadView()
textField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%@", textField.text);
return YES;
}
UML图如下:
5. UILabel
接下来我们在屏幕的随机位置添加UILabel。
- (void)drawHypnoticMessage:(NSString *)message
{
for (int i = 0; i < 20; i++)
{
UILabel *messageLabel = [[UILabel alloc] init];
// Configure the label's colors and text
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor = [UIColor whiteColor];
messageLabel.text = message;
// Resize the label relative to the text that it is displaying
[messageLabel sizeToFit];
// Get a random x value that fits within the hypnosis view's width
int width = (int)(self.view.bounds.size.width) - messageLabel.bounds.size.width;
int x = arc4random() % width;
// Get a random y value that fits within the hypnosis view's height
int height = (int)(self.view.bounds.size.height) - messageLabel.bounds.size.height;
int y = arc4random() % height;
// Update the label's frame
CGRect frame = messageLabel.frame;
frame.origin = CGPointMake(x, y);
messageLabel.frame = frame;
// Add the label to the hierarchy
[self.view addSubview:messageLabel];
}
}
修改textFieldShouldReturn方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//NSLog(@"%@", textField.text);
[self drawHypnoticMessage:textField.text];
textField.text = @"";
[textField resignFirstResponder];
return YES;
}
程序执行效果如下:
6. Motion效果
iOS设备有很多功能强大的模块,比如加速计,磁强器,陀螺仪等,用于辅助确定设备的方向是纵向的还是横向的。利用这些,iOS提供了一种很酷的特效:在主界面以不同的角度倾斜设备,应用程序图标相对于背景桌面在进行移动,UIInterpolatingMotinEffect类,就用于实现这个效果。修改drawHypnoticeMessage:,实现Motion特效。
UIInterpolatingMotionEffect *motionEffect;
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = @(-25);
motionEffect.maximumRelativeValue = @(25);
[messageLabel addMotionEffect:motionEffect];
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
motionEffect.minimumRelativeValue = @(-25);
motionEffect.maximumRelativeValue = @(25);
[messageLabel addMotionEffect:motionEffect];
7. 缩放
修改之前的一个例子,实现通过2个手指缩放scroll view,基本步骤如下:
- 设置UIScrollView的id delegate代理对象
- 设置minimumZoomScale :缩小的最小比例,这里需要注意,要enable缩放,最大缩放比例必需大于(不能等于)最小缩放比例
- 设置maximumZoomScale :放大的最大比例
- 让代理对象实现viewForZoomingInScrollView方法,返回需要缩放的视图控件 (UIView )viewForZoomingInScrollView:(UIScrollView )scrollView
声明是UIScrollView的代理:
@interface AppDelegate () <UIScrollViewDelegate>
@property (strong, nonatomic) BNRHypnosisView *hypnosisView;
@property (strong, nonatomic) UIScrollView *scrollView;
@end
设置zoom scale:
self.hypnosisView = [[BNRHypnosisView alloc] initWithFrame:screenRect];
[self.scrollView addSubview:self.hypnosisView];
self.scrollView.delegate = self;
// Turn off the paging for Zoom
self.scrollView.pagingEnabled = NO;
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.contentSize = screenRect.size;
实现代理方法:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.hypnosisView;
}
程序执行效果如下: