github:git
思路:遍歷鍵盤window的subviews,若是發現UIKBHandwritingView,則當前鍵盤爲手寫鍵盤;github
手寫鍵盤的位置:windows
UIRemoteKeyboardWindow 數組
UIInputSetContainerViewspa
UIInputSetHostViewblog
_UIKBCompatInputViewip
UIKeyboardAutomaticstring
UIKeyboardImplit
UIKeyboardLayoutStario
UIKBKeyplaneView
UIKBHandwritingView
UIKBHandwritingCandidateView
注意,上圖顯示UIKeyboardLayoutStar是UIKeyboardImpl的第一個view,可是iPad的實際以下圖:
// 思路:判斷有沒有UIKBHandwritingView,遍歷全部view
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
#warning 擔憂蘋果改變鍵盤結構,沒有取到UIKBHandwritingView;形成沒法消費!
NSString *yuan = @"鄧超界";
if (![string isEqualToString:yuan]) {
[SVProgressHUD showErrorWithStatus:@"簽名不是持卡人姓名,請重寫;若是不是手寫鍵盤,請長按或者點擊鍵盤上左下角的地球圖標,選擇 簡體手寫"];
textField.text = @"";
return NO;
}
if ([string isEqualToString:@"\n"]) {
return NO;
}
// 判斷有沒有UIKBHandwritingView 遍歷全部view
UIView *handwritingView;
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
if ([[keyboardWindow description] hasPrefix:@"<UIRemoteKeyboardWindow"] == YES) {
for (UIView *setContainer in keyboardWindow.subviews) {
if ([[setContainer description] hasPrefix:@"<UIInputSetContainerView"] == YES) {
for (UIView *setHost in setContainer.subviews) {
if ([[setHost description] hasPrefix:@"<UIInputSetHostView"] == YES) {
for (UIView *compat in setHost.subviews) {
if ([[compat description] hasPrefix:@"<_UIKBCompatInputView"] == YES) {
for (UIView *automatic in compat.subviews) {
if ([[automatic description] hasPrefix:@"<UIKeyboardAutomatic"] == YES) {
for (UIView *lmpl in automatic.subviews) {
if ([[lmpl description] hasPrefix:@"<UIKeyboardImpl"] == YES) {
for (UIView *layoutStar in lmpl.subviews) {
if ([[layoutStar description] hasPrefix:@"<UIKeyboardLayoutStar"] == YES) {
for (UIView *plane in layoutStar.subviews) {
if ([[plane description] hasPrefix:@"<UIKBKeyplaneView"] == YES) {
for (UIView *view in plane.subviews) {
if([[view description] hasPrefix:@"<UIKBHandwritingView"] == YES){
handwritingView = view;
break;
}
}
break;
}
}
break;
}
}
break;
}
}
break;
}
}
break;
}
}
break;
}
}
break;
}
}
break;
}
}
if (handwritingView) {
// 截屏
[self printscreenOfView:handwritingView];
[SVProgressHUD showSuccessWithStatus:@"簽名正確"];
}else
{
[SVProgressHUD showInfoWithStatus:@"請長按或者點擊鍵盤上左下角的地球圖標,切換鍵盤爲 簡體手寫"];
textField.text = @"";
return NO;
}
return YES;
}
for in,若是遍歷的數組元素個數爲零,不會執行遍歷;
break終止循環執行循環體下面的代碼return終止循環而且退出循環所在的方法continue終止當前循環,進行下一次循環