Skip to content

Commit 40e364c

Browse files
authored
Merge pull request #31 from tapwork/allows_all_orientation_fixes_layouting
Allows all orientation & fixes layouting
2 parents ba29a0f + bc1b7c2 commit 40e364c

2 files changed

Lines changed: 21 additions & 28 deletions

File tree

Sources/BarcodeScannerController.swift

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,6 @@ open class BarcodeScannerController: UIViewController {
203203
object: nil)
204204
}
205205

206-
open override func viewWillAppear(_ animated: Bool) {
207-
super.viewWillAppear(animated)
208-
209-
setupFrames()
210-
infoView.setupFrames()
211-
headerView.isHidden = !isBeingPresented
212-
}
213-
214206
open override func viewDidAppear(_ animated: Bool) {
215207
super.viewDidAppear(animated)
216208
animateFocusView()
@@ -268,7 +260,7 @@ open class BarcodeScannerController: UIViewController {
268260
output.metadataObjectTypes = metadata
269261
videoPreviewLayer?.session = captureSession
270262

271-
setupFrames()
263+
view.setNeedsLayout()
272264
}
273265

274266
// MARK: - Reset
@@ -310,25 +302,30 @@ open class BarcodeScannerController: UIViewController {
310302
}
311303

312304
// MARK: - Layout
305+
open override func viewWillLayoutSubviews() {
306+
super.viewWillLayoutSubviews()
313307

314-
/**
315-
Sets frames of the subviews.
316-
*/
317-
func setupFrames() {
318308
headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 64)
319309
flashButton.frame = CGRect(x: view.frame.width - 50, y: 73, width: 37, height: 37)
320310
infoView.frame = infoFrame
321311

322312
if let videoPreviewLayer = videoPreviewLayer {
323313
videoPreviewLayer.frame = view.layer.bounds
324-
325-
if videoPreviewLayer.connection != nil {
326-
videoPreviewLayer.connection.videoOrientation = .portrait
314+
if let connection = videoPreviewLayer.connection, connection.isVideoOrientationSupported {
315+
switch (UIApplication.shared.statusBarOrientation) {
316+
case .portrait: connection.videoOrientation = .portrait
317+
case .landscapeRight: connection.videoOrientation = .landscapeRight
318+
case .landscapeLeft: connection.videoOrientation = .landscapeLeft
319+
case .portraitUpsideDown: connection.videoOrientation = .portraitUpsideDown
320+
default: connection.videoOrientation = .portrait
321+
}
327322
}
328323
}
329324

330325
center(subview: focusView, inSize: CGSize(width: 218, height: 150))
331326
center(subview: settingsButton, inSize: CGSize(width: 150, height: 50))
327+
328+
headerView.isHidden = !isBeingPresented
332329
}
333330

334331
/**
@@ -345,12 +342,6 @@ open class BarcodeScannerController: UIViewController {
345342
height: size.height)
346343
}
347344

348-
// MARK: - Orientation
349-
350-
open override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
351-
return .portrait
352-
}
353-
354345
// MARK: - Animations
355346

356347
/**
@@ -386,13 +377,13 @@ open class BarcodeScannerController: UIViewController {
386377
focusView.layer.removeAllAnimations()
387378
focusView.isHidden = false
388379

389-
setupFrames()
390-
391380
UIView.animate(withDuration: 1.0, delay:0,
392381
options: [.repeat, .autoreverse, .beginFromCurrentState],
393382
animations: {
394383
self.center(subview: self.focusView, inSize: CGSize(width: 280, height: 80))
395384
}, completion: nil)
385+
386+
view.setNeedsLayout()
396387
}
397388

398389
// MARK: - Actions

Sources/InfoView.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class InfoView: UIVisualEffectView {
3636
*/
3737
var status: Status = Status(state: .scanning) {
3838
didSet {
39-
setupFrames()
39+
setNeedsLayout()
4040

4141
let stateStyles = status.state.styles
4242

@@ -79,7 +79,9 @@ class InfoView: UIVisualEffectView {
7979
/**
8080
Sets up frames of subviews.
8181
*/
82-
func setupFrames() {
82+
override func layoutSubviews() {
83+
super.layoutSubviews()
84+
8385
let padding: CGFloat = 10
8486
let labelHeight: CGFloat = 40
8587
let imageSize = CGSize(width: 30, height: 27)
@@ -127,7 +129,7 @@ class InfoView: UIVisualEffectView {
127129
borderView.isHidden = false
128130

129131
animate(blurStyle: .light)
130-
animate(borderViewAngle: CGFloat(M_PI_2))
132+
animate(borderViewAngle: CGFloat(Double.pi/2))
131133
}
132134

133135
/**
@@ -164,7 +166,7 @@ class InfoView: UIVisualEffectView {
164166
animations: {
165167
self.borderView.transform = CGAffineTransform(rotationAngle: borderViewAngle)
166168
}, completion: { [weak self] _ in
167-
self?.animate(borderViewAngle: borderViewAngle + CGFloat(M_PI_2))
169+
self?.animate(borderViewAngle: borderViewAngle + CGFloat(Double.pi/2))
168170
})
169171
}
170172
}

0 commit comments

Comments
 (0)