版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.08.06 |
前言
在app中,很多時候都需要在輸入框里面輸入東西,這個時候鍵盤彈出,不可避免的就會有遮擋頁面的問題,這一篇就講一下解決鍵盤遮擋頁面的方法。感興趣的可以看看我寫的其他小技巧。
1. 實用小技巧(一):UIScrollView中上下左右滾動方向的判斷
2. 實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯
3.實用小技巧(三):點擊手勢屏蔽子視圖的響應
4.實用小技巧(四):動態的增刪標簽視圖
5.實用小技巧(五):通過相冊或者相機更改圖標
6.實用小技巧(六):打印ios里所有字體
7. 實用小技巧(七):UITableViewCell自適應行高的計算
8. 實用小技巧(八):數字余額顯示的分隔
9.實用小技巧(九):類頭條模糊背景的實現
10.實用小技巧(十):晃動手機換后臺服務器網絡
11.實用小技巧(十一):scrollView及其子類顯示的一些異常處理
12.實用小技巧(十二):頭像圖片縮放以及保存到相冊簡單功能的實現
13.實用小技巧(十三):一種類酷我音樂盒動畫實現
14.實用小技巧(十四):生成跳往applestore指定app的方法
15.實用小技巧(十五):左側向右滑動返回上一級控制器
16.實用小技巧(十六):獲取設備信息
17.實用小技巧(十七):清除緩存目錄
18.實用小技巧(十八):取出gif圖的每一幀
19.實用小技巧(十九):獲取相機和麥克風權限
20.實用小技巧(二十):游客模式的實現
21.實用小技巧(二十一):版本檢測的實現
22.實用小技巧(二十二):鍵盤遮擋問題
功能要求
獲取設備的閃光燈的權限,并可控制閃光燈的關閉。
功能實現
1. 幾個重要的類
AVCaptureSession
AVCaptureDevice
AVCaptureDeviceInput
AVCaptureSession
/*!
@class AVCaptureSession
@abstract
AVCaptureSession is the central hub of the AVFoundation capture classes.
@discussion
To perform a real-time capture, a client may instantiate AVCaptureSession and add appropriate AVCaptureInputs, such as AVCaptureDeviceInput, and outputs, such as AVCaptureMovieFileOutput. [AVCaptureSession startRunning] starts the flow of data from the inputs to the outputs, and [AVCaptureSession stopRunning] stops the flow. A client may set the sessionPreset property to customize the quality level or bitrate of the output.
*/
NS_CLASS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED
@interface AVCaptureSession : NSObject
{
@private
AVCaptureSessionInternal *_internal;
}
它是AVFoundation
框架中最重要的類。
AVCaptureDevice
/*!
@class AVCaptureDevice
@abstract
An AVCaptureDevice represents a physical device that provides realtime input media data, such as video and audio.
@discussion
Each instance of AVCaptureDevice corresponds to a device, such as a camera or microphone. Instances of AVCaptureDevice cannot be created directly. An array of all currently available devices can also be obtained using the AVCaptureDeviceDiscoverySession. Devices can provide one or more streams of a given media type. Applications can search for devices matching desired criteria by using AVCaptureDeviceDiscoverySession, or may obtain a reference to the default device matching desired criteria by using +[AVCaptureDevice defaultDeviceWithDeviceType:mediaType:position:].
Instances of AVCaptureDevice can be used to provide media data to an AVCaptureSession by creating an AVCaptureDeviceInput with the device and adding that to the capture session.
*/
NS_CLASS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED
@interface AVCaptureDevice : NSObject
{
@private
AVCaptureDeviceInternal *_internal;
}
所有視頻、音頻以及相關硬件設備的獲取都在這個類中,這里就使用其兩個相關的枚舉,如下:
typedef NS_ENUM(NSInteger, AVCaptureTorchMode) {
AVCaptureTorchModeOff = 0,
AVCaptureTorchModeOn = 1,
AVCaptureTorchModeAuto = 2,
} NS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED;
typedef NS_ENUM(NSInteger, AVCaptureFlashMode) {
AVCaptureFlashModeOff = 0,
AVCaptureFlashModeOn = 1,
AVCaptureFlashModeAuto = 2
} NS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED;
AVCaptureDeviceInput
/*!
@class AVCaptureDeviceInput
@abstract
AVCaptureDeviceInput is a concrete subclass of AVCaptureInput that provides an interface for capturing media from an AVCaptureDevice.
@discussion
Instances of AVCaptureDeviceInput are input sources for AVCaptureSession that provide media data from devices connected to the system, represented by instances of AVCaptureDevice.
*/
NS_CLASS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED
@interface AVCaptureDeviceInput : AVCaptureInput
{
@private
AVCaptureDeviceInputInternal *_internal;
}
2. 代碼實現
下面我們直接看代碼。
#import "JJTorchVC.h"
#import <AVFoundation/AVFoundation.h>
#import "Masonry.h"
@interface JJTorchVC ()
@property (nonatomic, strong) UIButton *switchButton;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureDevice *captureDevice;
@end
@implementation JJTorchVC
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self setupUI];
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
self.captureSession = captureSession;
NSArray *allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice *currentDevice in allDevices) {
if (currentDevice.position == AVCaptureDevicePositionBack) {
self.captureDevice = currentDevice;
}
}
AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice error:nil];
[self.captureSession addInput:deviceInput];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
#pragma mark - Object Private Function
- (void)setupUI
{
UIButton *switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
switchButton.backgroundColor = [UIColor lightTextColor];
[switchButton addTarget:self action:@selector(switchButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
[switchButton setImage:[UIImage imageNamed:@"torch_off"] forState:UIControlStateNormal];
[switchButton setImage:[UIImage imageNamed:@"torch_on"] forState:UIControlStateSelected];
[self.view addSubview:switchButton];
self.switchButton = switchButton;
[self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.height.equalTo(@100);
}];
}
#pragma mark - Action && Notification
- (void)switchButtonDidClick:(UIButton *)button
{
button.selected = !button.selected;
NSLog(@"%d",button.isSelected);
if ([self.captureDevice hasTorch] && [self.captureDevice hasFlash]) {
if (button.isSelected) {
NSLog(@"開關已打開");
if (self.captureDevice.torchMode == AVCaptureTorchModeOff) {
[self.captureSession beginConfiguration];
[self.captureDevice lockForConfiguration:nil];
[self.captureDevice setTorchMode:AVCaptureTorchModeOn];
[self.captureDevice setFlashMode:AVCaptureFlashModeOn];
[self.captureDevice unlockForConfiguration];
[self.captureSession commitConfiguration];
}
}
else {
NSLog(@"開關已關閉");
if (self.captureDevice.torchMode == AVCaptureTorchModeOn) {
[self.captureSession beginConfiguration];
[self.captureDevice lockForConfiguration:nil];
[self.captureDevice setTorchMode:AVCaptureTorchModeOff];
[self.captureDevice setFlashMode:AVCaptureFlashModeOff];
[self.captureDevice unlockForConfiguration];
[self.captureSession commitConfiguration];
[self.captureSession stopRunning];
}
}
}
}
@end
功能效果
這里只能給兩張頁面圖示,不能給閃光燈打開的圖示了。
后記
這個就暫時寫這么多了,都是很基礎的東西,后續會加深難度和展示更炫酷的功能。