為了讓我們的app更加安全赊舶,需要數(shù)字隨機的密碼鍵盤精耐,這就需要自定義鍵盤了.
大體的思路
自定義鍵盤思路.png
打亂數(shù)組
- (NSMutableArray *)upsetOrderMutableArrary{
NSMutableArray *arrary = [[NSMutableArray alloc] initWithArray:self];
[arrary sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return (NSInteger)arc4random() % 3 - 1;
}];
return arrary;
}
如何獲取第一響應(yīng)者
static __weak id safe_currentFirstResponder;
@implementation UIResponder (ISTSafeKeyboardFirstResponder)
+ (id)safe_currentFirstResponder{
safe_currentFirstResponder = nil;
/*
UIWindow keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView firstResponder = [keyWindow performSelector:@selector(firstResponder)];
*/
//以上方法調(diào)用了私有api粪摘,審核會被拒絕魂莫, 用遞歸也可以找到第一響應(yīng)者隅熙,但是有點呵呵
// 通過將target設(shè)置為nil沐旨,讓系統(tǒng)自動遍歷響應(yīng)鏈
// 從而響應(yīng)鏈當(dāng)前第一響應(yīng)者響應(yīng)我們自定義的方法
[[UIApplication sharedApplication] sendAction:@selector(safe_findFirstResponder:)
to:nil
from:nil
forEvent:nil];
return safe_currentFirstResponder;
}
- (void)safe_findFirstResponder:(id)sender {
// 第一響應(yīng)者會響應(yīng)這個方法,并且將靜態(tài)變量wty_currentFirstResponder設(shè)置為自己
safe_currentFirstResponder = self;
}
@end
最后demo附上
最后的效果圖:
屏幕快照 2017-09-06 下午5.24.42.png