自定義鍵盤
主要思路:自定義鍵盤就是重新加載textfield的inputView
自定義一個可以從相冊或者拍照來上傳圖片的鍵盤
第一步:對鍵盤進行監聽
//增加監聽,當鍵盤出現時收出消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//增加監聽,當鍵盤退出時收出消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
//增加監聽,當鍵盤改變時收出消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
//初始化一個View,用來放“照片”按鈕
specView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDTH, 30)];
specView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:specView];
第二步:
#pragma mark -- 當鍵盤出現時調用
-(void)keyboardWillShow:(NSNotification *)noti {
//獲取鍵盤高度
NSDictionary *userInfo = noti.userInfo;
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
//創建“照片”按鈕
UIButton *photoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
photoBtn.frame = CGRectMake(SP_W(20), 5, SP_W(30), 20);
[photoBtn setTitle:@"照片" forState:UIControlStateNormal];
[photoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
photoBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[photoBtn addTarget:self action:@selector(ShowPhoto:) forControlEvents:UIControlEventTouchUpInside];
[specView addSubview:photoBtn];
//改變specView的位置,在鍵盤上面
[UIView animateWithDuration:0.1 animations:^{
specView.frame = CGRectMake(0, HEIGHT - height - 30, WIDTH, 30);
fabiaoText.frame = CGRectMake(SP_W(10), 665 - height - 50 + 30, WIDTH - SP_W(20), 40);
}];
}
第三步:
#pragma mark -- 點擊“照片”按鈕事件
-(void)ShowPhoto:(UIButton *)button {
if (isPhoto == NO) {
[self layoutPhotoKeyboard];
//將textfield的inputView設置成自定義的視圖
textfield.inputView = photoView;
//重新加載inputView
[textfield reloadInputViews];
isPhoto = YES;
}else {
//此時將textfield的inputView設置成nil,鍵盤會重新換成系統鍵盤
textfield.inputView = nil;
//重新加載inputView
[textfield reloadInputViews];
isPhoto = NO;
}
}
#pragma mark -- 自定義鍵盤的樣式
-(void)layoutPhotoKeyboard {
//背景
photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 160)];
//相冊按鈕
UIButton *xiangceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
xiangceBtn.frame = CGRectMake(SP_W(10), 10, SP_W(50), SP_W(50));
[xiangceBtn setTitle:@"相冊" forState:UIControlStateNormal];
[xiangceBtn addTarget:self action:@selector(FromXiangce:) forControlEvents:UIControlEventTouchUpInside];
[photoView addSubview:xiangceBtn];
//拍照按鈕
UIButton *paizhaoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
paizhaoBtn.frame = CGRectMake(CGRectGetMaxX(xiangceBtn.frame) + SP_W(20), 10, SP_W(50), SP_W(50));
[paizhaoBtn setTitle:@"拍照" forState:UIControlStateNormal];
[paizhaoBtn addTarget:self action:@selector(FromPaiZhao:) forControlEvents:UIControlEventTouchUpInside];
[photoView addSubview:paizhaoBtn];
}
第四步:分別從相冊和拍照獲取照片
#pragma mark -- 照片 從相冊獲取
-(void)FromXiangce:(UIButton *)button {
//創建對象
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//(選擇類型)表示僅僅從相冊中選取照片
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//指定代理,因此我們要實現UIImagePickerControllerDelegate,
// UINavigationControllerDelegate協議
imagePicker.delegate = self;
//設置在相冊選完照片後,是否跳到編輯模式進行圖片剪裁。(允許用戶編輯)
imagePicker.allowsEditing = YES;
//顯示相冊
[self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark -- 照片 拍照獲取
-(void)FromPaiZhao:(UIButton *)button {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES; //允許用戶編輯
[self presentViewController:imagePicker animated:YES completion:nil];
} else {
//彈出窗口響應點擊事件
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告"
message:@"未檢測到攝像頭" delegate:nil cancelButtonTitle:nil
otherButtonTitles:@"確定", nil, nil];
[alert show];
}
}
//圖片完成之後處理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
//image 就是修改後的照片
//將image放到你想要放的地方就行了
//結束操作
[self dismissViewControllerAnimated:YES completion:nil];
}
自定義表情鍵盤和上面的原理一樣,只是代碼比這個稍微複雜點,稍後整理。。。
效果圖:灰色部分是鍵盤,再點擊一次照片,會換成系統鍵盤
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.