Skip to content

Commit

Permalink
fix issues with change camera button
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalmoody committed Jun 26, 2024
1 parent 66da5fd commit 3bf94d2
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions iOS/GBViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ @implementation GBViewController
NSTimer *_disableCameraTimer;
AVCaptureDevicePosition _cameraPosition;
UIButton *_cameraPositionButton;
UIButton *_changeCameraButton;
NSArray *_allCaptureDevices;
NSArray *_backCaptureDevices;
AVCaptureDevice *_selectedCaptureDevice;
UIButton *_changeCameraButton;
AVCaptureDevice *_selectedBackCaptureDevice;

__weak GCController *_lastController;

Expand Down Expand Up @@ -245,7 +245,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

_motionManager = [[CMMotionManager alloc] init];
_cameraPosition = AVCaptureDevicePositionBack;
_selectedCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
_selectedBackCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];

// Back camera setup
NSArray *deviceTypes = @[AVCaptureDeviceTypeBuiltInWideAngleCamera,
Expand Down Expand Up @@ -273,8 +273,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}
_backCaptureDevices = filteredBackCameras;

_cameraPositionButton = [[UIButton alloc] initWithFrame:CGRectMake(8,
0,
UIEdgeInsets insets = self.window.safeAreaInsets;
_cameraPositionButton = [[UIButton alloc] initWithFrame:CGRectMake(insets.left + 8,
_backgroundView.bounds.size.height - 8 - insets.bottom - 32,
32,
32)];
[self didRotateFromInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation];
Expand All @@ -284,20 +285,20 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
forState:UIControlStateNormal];
_cameraPositionButton.backgroundColor = [UIColor systemBackgroundColor];

// Configure the change camera button
_changeCameraButton = [[UIButton alloc] initWithFrame:CGRectMake(8,
0,
// Configure the change camera button stacked on top of the camera position button
_changeCameraButton = [[UIButton alloc] initWithFrame:CGRectMake(insets.left + 8,
_backgroundView.bounds.size.height - 8 - insets.bottom - 32 - 32 - 8,
32,
32)];
[_changeCameraButton setImage:[UIImage systemImageNamed:@"camera"
[_changeCameraButton setImage:[UIImage systemImageNamed:@"camera.aperture"
withConfiguration:[UIImageSymbolConfiguration configurationWithScale:UIImageSymbolScaleLarge]]
forState:UIControlStateNormal];
_changeCameraButton.backgroundColor = [UIColor systemBackgroundColor];
_changeCameraButton.layer.cornerRadius = 6;
_changeCameraButton.alpha = 0;
[_changeCameraButton addTarget:self
action:@selector(changeCamera)
forControlEvents:UIControlEventTouchUpInside];
action:@selector(changeCamera)
forControlEvents:UIControlEventTouchUpInside];
// Only show the change camera button if we have more than one back camera to swap between.
if ([_backCaptureDevices count] > 1) {
[_backgroundView addSubview:_changeCameraButton];
Expand All @@ -309,11 +310,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
forState:UIControlStateNormal];
_cameraPositionButton.backgroundColor = [UIColor whiteColor];
}

_cameraPositionButton.layer.cornerRadius = 6;
_cameraPositionButton.alpha = 0;
[_cameraPositionButton addTarget:self
action:@selector(rotateCamera)
forControlEvents:UIControlEventTouchUpInside];
action:@selector(rotateCamera)
forControlEvents:UIControlEventTouchUpInside];

[_backgroundView addSubview:_cameraPositionButton];

Expand Down Expand Up @@ -737,9 +739,15 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation dur
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIEdgeInsets insets = self.window.safeAreaInsets;
_cameraPositionButton.frame = CGRectMake(insets.left + 8, _backgroundView.bounds.size.height - 8 - insets.bottom - 32, 32, 32);
if (@available(iOS 13.0, *)) {
_changeCameraButton.frame = CGRectMake(_backgroundView.bounds.size.width - 8 - insets.right - 32, _backgroundView.bounds.size.height - 8 - insets.bottom - 32, 32, 32);
_cameraPositionButton.frame = CGRectMake(insets.left + 8,
_backgroundView.bounds.size.height - 8 - insets.bottom - 32,
32,
32);
if (_changeCameraButton != nil) {
_changeCameraButton.frame = CGRectMake(insets.left + 8,
_backgroundView.bounds.size.height - 8 - insets.bottom - 32 - 32 - 8,
32,
32);
}
}

Expand Down Expand Up @@ -1186,7 +1194,7 @@ - (AVCaptureDevice *)captureDevice
}

// There may be several back cameras, return the one with the matching type
if ([device deviceType] == [_selectedCaptureDevice deviceType]) {
if ([device deviceType] == [_selectedBackCaptureDevice deviceType]) {
return device;
}
}
Expand Down Expand Up @@ -1242,6 +1250,14 @@ - (void)cameraRequestUpdate
_cameraPositionButton.alpha = 1;
}];
}
if (_changeCameraButton != nil) {
// The change camera button is only available when we are using a capture device on the back of the device
int changeCameraButtonAlpha = (_cameraPosition == AVCaptureDevicePositionFront) ? 0 : 1;
[UIView animateWithDuration:0.25 animations:^{
_changeCameraButton.alpha = changeCameraButtonAlpha;
}];
}

_disableCameraTimer = [NSTimer scheduledTimerWithTimeInterval:1
repeats:false
block:^(NSTimer *timer) {
Expand All @@ -1250,6 +1266,13 @@ - (void)cameraRequestUpdate
_cameraPositionButton.alpha = 0;
}];
}
if (_changeCameraButton != nil) {
if (_changeCameraButton.alpha) {
[UIView animateWithDuration:0.25 animations:^{
_changeCameraButton.alpha = 0;
}];
}
}
dispatch_async(_cameraQueue, ^{
[_cameraSession stopRunning];
_cameraSession = nil;
Expand Down Expand Up @@ -1359,9 +1382,9 @@ - (void)changeCamera
{
dispatch_async(_cameraQueue, ^{
// Get index of selected camera and select the next one, wrapping to the beginning
NSUInteger i = [_backCaptureDevices indexOfObject:_selectedCaptureDevice];
NSUInteger i = [_backCaptureDevices indexOfObject:_selectedBackCaptureDevice];
int nextIndex = (i + 1) % _backCaptureDevices.count;
_selectedCaptureDevice = _backCaptureDevices[nextIndex];
_selectedBackCaptureDevice = _backCaptureDevices[nextIndex];

[_cameraSession stopRunning];
_cameraSession = nil;
Expand Down

0 comments on commit 3bf94d2

Please sign in to comment.