rsc
rsc

Reputation: 10679

change AVCaptureVideoPreviewLayer position instantly

I have an AVCaptureVideoPreviewLayer on screen. I want to change its position instantly after the user touches a button.

I am able to change its position using the following code, but it is not happening instantly but with some kind of smooth liner transition.

CGRect frame = self.preview.frame;
frame.origin.x = previewLeft;
frame.origin.y = previewTop;
self.preview.frame = frame;

How can I make it to change its position without any kind of intrinsic animation?

Upvotes: 1

Views: 508

Answers (2)

CIFilter
CIFilter

Reputation: 8677

CALayer.frame is a computed property, but it cannot be explicitly animated. By setting it, you're also setting position, which is implicitly animated.

You can temporarily suppress all actions, the mechanism by which Core Animation creates its default layer animations:

Swift code:

CATransaction.begin()
CATransaction.setDisableActions(true)

self.preview.frame = frame;

CATransaction.commit()

Objective-C code:

[CATransaction begin];
[CATransaction setDisableActions:YES];

self.preview.frame = frame;

[CATransaction commit];

Upvotes: 2

ferrisxie
ferrisxie

Reputation: 26

LucasTizma's answer is perfect.

Besides, there is a trick to solve this issue.

Just slow down the window speed:

[(CALayer *)[[[[UIApplication sharedApplication] windows] objectAtIndex:0] layer] setSpeed:0.9];

Don't forget to correct it to 1.0f after your transition.

Upvotes: 1

Related Questions