系統鍵盤在密碼框輸入時,如果用戶開啟錄屏,鍵盤在錄屏得到的視頻里會不可見,但是用戶在錄屏時卻能看到,
為了實作這個效果,利用UItextfield在錄屏下視頻不可見的特性,將實作這一效果的私有UIview,也就是_UITextLayoutCanvasView提取出來,作為背景,其他組件在這個背景上顯示,objective c代碼如下
-(UIView *)initWithFrame:(CGRect)frame{
UITextField * root=[[UITextField alloc] initWithFrame:frame];
if(root){
root.secureTextEntry=YES;//利用密碼框錄屏不可見的特性
UIView *textLayoutCanvasView=root.subviews.firstObject;//獲取_UITextLayoutCanvasView
if (textLayoutCanvasView.subviews.count) {
[textLayoutCanvasView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
textLayoutCanvasView.frame=frame;
textLayoutCanvasView.userInteractionEnabled=YES;
textLayoutCanvasView.backgroundColor=[UIColor clearColor];
return textLayoutCanvasView;
}
return root.subviews.firstObject;
}
需要注意的是,目前是利用通過獲取指定位置的 UITextField 的 subview 來獲取私有 view ,如果后期 UITextField 的 subview 位置變更就要再做處理,而且_UITextLayoutCanvasView作為私有成員是不能被添加成員變數和函式的,只能作為背景,例如要實作自定義鍵盤和系統鍵盤類似的錄屏不可見效果,我目前只能這樣想到做:
CustomKeyboard *keyboard=[[CustomKeyboard alloc]init];//你的自定義鍵盤類
UIView *shield=[self initWithFrame:keyboard.frame];//實作錄屏不可見的_UITextLayoutCanvasView
[shield addSubview:keyboard];//讓鍵盤基于_UITextLayoutCanvasView
shield.userInteractionEnabled=YES;//允許互動,不然無法點擊
UITexrField.inputView=shield;//設定UITextField的inputView
然后如果出現點擊事件傳遞不到button的原因,是因為事件在更上一層view被攔截了,傳遞touch或者直接在更上一層處理都行,
swift實作請參考:
https://juejin.cn/post/7066341701815631909#heading-8
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439196.html
標籤:iOS
