IOS添加手動對焦功能

1.初始化

/*
 * 對焦手勢
 */
_tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(focusGesture:)];
[self.view addGestureRecognizer:_tapGesture];

/*
 * 對焦效果view
 */
_focusView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
_focusView.layer.borderWidth = 1.0;
_focusView.layer.borderColor =[UIColor greenColor].CGColor;
_focusView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_focusView];
_focusView.hidden = YES;

2.focusGesture實現

- (void)focusGesture:(UITapGestureRecognizer*)gesture{
    CGPoint point = [gesture locationInView:gesture.view];
    [self focusAtPoint:point];
}

- (void)focusAtPoint:(CGPoint)point{
    CGSize size = self.view.bounds.size;
    CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );
    NSError *error;
    if ([_captureManager.device lockForConfiguration:&error]) {
        if ([_captureManager.device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
            [_captureManager.device setFocusPointOfInterest:focusPoint];
            [_captureManager.device setFocusMode:AVCaptureFocusModeAutoFocus];
        }
        [_captureManager.device unlockForConfiguration];
    }
    /*
     * 下面是手觸碰屏幕後對焦的效果
     */
    _focusView.center = point;
    _focusView.hidden = NO;

    [UIView animateWithDuration:0.3 animations:^{
        _focusView.transform = CGAffineTransformMakeScale(1.25, 1.25);
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            _focusView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            _focusView.hidden = YES;
        }];
    }];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章