iphone应用程序开发指南--触摸事件 - 三戒1993
iphone应用程序开发指南–触摸事件
2013-04-08 10:40
三戒1993
阅读(87)
评论(0)
编辑
收藏
举报
类中有一个名为locationInView:的重要方法,如果传入self 参数值,它会给出触摸动作在响应者坐标系统中的位置(假定该响应者是一个UIView 对象,且传入的视图参数不为nil)。另外,还有一个与之平行的方法,可以给出触摸动作之前位置(previousLocationInView:)。UITouch
实例的属性还可以给出发生多少次触
图像是否被触碰。
方法来创建自己的应用程序粘贴板。如果您调用pasteboardWithUniqueName 方法,UIPasteboard 会为您提供一个具有唯一名称的应用程序粘贴板。您可以通过其name 属性声明来取得这个名称。
您可以通过调用视图对象的becomeFirstResponder 方法来为可编辑的文本视图显
示键盘。调用这个方法可以使目标视图成为第一响应者,并开始编辑过程,其效果和用户触击该视图是一样的。
需要做的调整通常包括暂时调整一或多个视图的尺寸和位置,从而使文本对象可见。管理带
有键盘的文本对象的最简单方法是将它们嵌入到一个UIScrollView ( 或其子类,如UITableView)对象。当键盘被显示出来时,您需要做的只是调整滚动视图的尺寸,并将目标文本对象滚动到合适的位置。为此,在UIKeyboardDidShowNotification 通告的处理代码中需要进行如下操作:
取得键盘的尺寸。
将滚动视图的高度减去键盘的高度。
将目标文本框滚动到视图中。
在配置滚动视图时,请务必为所有的内容视图配置恰当的自动尺寸调整规则。在之
前的图中,文本框实际上是一个UIView 对象的子视图,该UIView 对象又是UIScrollView对象的子视图。如果该UIView 对象的UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleHeight 选项被设置了,则改变滚动视图的边框尺寸会同时改变它的边框,因而可能导致不可预料的结果。禁用这些选项可以确保该视图保持尺寸不变,并正确滚动。
处理键盘通告
// Call this method somewhere in your view controller setup code.
– (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
– (void)keyboardWasShown:(NSNotification*)aNotification
{
if (keyboardShown)
return;
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info
objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [scrollView frame];
viewFrame.size.height -= keyboardSize.height;
scrollView.frame = viewFrame;
// Scroll the active text field into view.
CGRect textFieldRect = [activeField frame];
[scrollView scrollRectToVisible:textFieldRect animated:YES];
keyboardShown = YES;
}
// Called when the UIKeyboardDidHideNotification is sent
– (void)keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info
objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Reset the height of the scroll view to its original value
CGRect viewFrame = [scrollView frame];
viewFrame.size.height += keyboardSize.height;
scrollView.frame = viewFrame;
keyboardShown = NO;
}
跟踪活动文本框的方法
– (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
– (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}