Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SDK: How to cause view to flip when switching cameras

Fairly new at iOS/Objective C. I'm making mods to Apple's AVCam (video capture) sample code and would like to imitate the native camera's flip animation when switching between front and back cameras. It seems as if this would be easy but I can't get a handle on how it's done. Advice would be welcome.

Thanks!

Mark

like image 226
mpemburn Avatar asked May 08 '12 02:05

mpemburn


People also ask

How do I flip the screen on my iPhone while recording?

Answer: A: You don't. Changing from front to rear camera while recording video is not a feature of the iPhone. Check the App Store, there are some apps that will allow that.

How do I change my iPhone from front to back camera?

Flipping the cameraTap the Flip Camera button in the bottom-right corner to switch between front- and rear-facing cameras. You can use the front-facing camera to take a photo of yourself. You'll also use the front-facing camera for video-conferencing apps like FaceTime.

How do you flip the camera while taking a video?

On Android, go to the Video option. Press the Capture icon to begin recording. Then double-tap anywhere on the screen to flip the camera. Once you are done recording, tap on the Save icon to download the video.


1 Answers

It is indeed a simple task. All you need to do is call the method transitionWithView from UIView right before changing the previewView input.

If you're targeting iOS 8.0 or later, you can also easily add a blur effect.

Swift 2

let blurView = UIVisualEffectView(frame: previewView.bounds)
blurView.effect = UIBlurEffect(style: .Light)
previewView.addSubview(blurView)

UIView.transitionWithView(previewView, duration: 0.4, options: .TransitionFlipFromLeft, animations: nil) { (finished) -> Void in
    blurView.removeFromSuperview()
}

Swift 3, 4, 5

let blurView = UIVisualEffectView(frame: previewView.bounds)
blurView.effect = UIBlurEffect(style: .light)
previewView.addSubview(blurView)

UIView.transition(with: previewView, duration: 0.4, options: .transitionFlipFromLeft, animations: nil) { (finished) -> Void in
    blurView.removeFromSuperview()
}

Objective-C

UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithFrame:_previewView.bounds];
blurView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
[_previewView addSubview:blurView];

[UIView transitionWithView:_previewView duration:0.4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) {
    [blurView removeFromSuperview];
}];
like image 164
brnunes Avatar answered Sep 29 '22 07:09

brnunes