From 5e63a926645ceeb58ca442c5470348061f92a1b0 Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Fri, 4 Sep 2015 12:09:25 +0200 Subject: [PATCH] Migrate to Swift 2 --- .../ImagePickerDemo.xcodeproj/project.pbxproj | 1 + Source/BottomView/BottomContainerView.swift | 4 +-- Source/BottomView/ButtonPicker.swift | 4 +-- Source/BottomView/ImageStack.swift | 2 +- Source/BottomView/StackView.swift | 10 +++--- Source/CameraView/CameraView.swift | 36 ++++++++++++------- Source/ImageGallery/ImageGalleryView.swift | 8 ++--- .../ImageGallery/ImageGalleryViewCell.swift | 2 +- Source/ImagePickerController.swift | 4 +-- Source/TopView/TopView.swift | 4 +-- 10 files changed, 44 insertions(+), 31 deletions(-) diff --git a/Demo/ImagePickerDemo/ImagePickerDemo.xcodeproj/project.pbxproj b/Demo/ImagePickerDemo/ImagePickerDemo.xcodeproj/project.pbxproj index 12b028e7..0d28153d 100644 --- a/Demo/ImagePickerDemo/ImagePickerDemo.xcodeproj/project.pbxproj +++ b/Demo/ImagePickerDemo/ImagePickerDemo.xcodeproj/project.pbxproj @@ -180,6 +180,7 @@ 29D699D11B70ABFC0021FA73 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0640; ORGANIZATIONNAME = "Ramon Gilabert Llop"; TargetAttributes = { diff --git a/Source/BottomView/BottomContainerView.swift b/Source/BottomView/BottomContainerView.swift index 350069d3..426031f4 100644 --- a/Source/BottomView/BottomContainerView.swift +++ b/Source/BottomView/BottomContainerView.swift @@ -70,7 +70,7 @@ class BottomContainerView: UIView { for view in [borderPickerButton, pickerButton, doneButton, stackView, topSeparator] { addSubview(view) - view.setTranslatesAutoresizingMaskIntoConstraints(false) + view.translatesAutoresizingMaskIntoConstraints = false } backgroundColor = configuration.backgroundColor @@ -79,7 +79,7 @@ class BottomContainerView: UIView { setupConstraints() } - required init(coder aDecoder: NSCoder) { + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/Source/BottomView/ButtonPicker.swift b/Source/BottomView/ButtonPicker.swift index 60048274..3d908187 100644 --- a/Source/BottomView/ButtonPicker.swift +++ b/Source/BottomView/ButtonPicker.swift @@ -15,7 +15,7 @@ class ButtonPicker: UIButton { lazy var numberLabel: UILabel = { [unowned self] in let label = UILabel() - label.setTranslatesAutoresizingMaskIntoConstraints(false) + label.translatesAutoresizingMaskIntoConstraints = false label.font = self.configuration.numberLabelFont return label @@ -61,7 +61,7 @@ class ButtonPicker: UIButton { object: nil) } - required init(coder aDecoder: NSCoder) { + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/Source/BottomView/ImageStack.swift b/Source/BottomView/ImageStack.swift index 3c7134f2..e715b36d 100644 --- a/Source/BottomView/ImageStack.swift +++ b/Source/BottomView/ImageStack.swift @@ -27,6 +27,6 @@ public class ImageStack { } public func containsImage(image: UIImage) -> Bool { - return contains(images, image) + return images.contains(image) } } diff --git a/Source/BottomView/StackView.swift b/Source/BottomView/StackView.swift index 7289c9fd..ccbb3bf1 100644 --- a/Source/BottomView/StackView.swift +++ b/Source/BottomView/StackView.swift @@ -38,7 +38,7 @@ class ImageStackView: UIView { layoutSubviews() } - required init(coder aDecoder: NSCoder) { + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @@ -74,10 +74,10 @@ class ImageStackView: UIView { let offset = -step * CGFloat(views.count) var origin = CGPoint(x: offset, y: offset) - for (i, view) in enumerate(views) { + for (i, view) in views.enumerate() { origin.x += step origin.y += step - var frame = CGRect(origin: origin, size: viewSize) + let frame = CGRect(origin: origin, size: viewSize) view.frame = frame } } @@ -116,9 +116,9 @@ extension ImageStackView { return } - let photos = suffix(images, 4) + let photos = images.suffix(4) - for (index, view) in enumerate(views) { + for (index, view) in views.enumerate() { if index <= photos.count - 1 { view.image = photos[index] view.alpha = 1 diff --git a/Source/CameraView/CameraView.swift b/Source/CameraView/CameraView.swift index 4c91494b..48987d55 100644 --- a/Source/CameraView/CameraView.swift +++ b/Source/CameraView/CameraView.swift @@ -111,14 +111,17 @@ class CameraView: UIViewController { delegate?.handleFlashButton(captureDevice?.position == .Front) - var error: NSError? = nil - UIView.animateWithDuration(0.3, animations: { [unowned self] in self.containerView.alpha = 1 }, completion: { finished in self.captureSession.beginConfiguration() self.captureSession.removeInput(currentDeviceInput) - self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice, error: &error)) + + let captureDeviceInput: AVCaptureDeviceInput? + do { try captureDeviceInput = AVCaptureDeviceInput(device: self.captureDevice) + } catch { } + + self.captureSession.addInput(AVCaptureDeviceInput(device: captureDeviceInput)) self.captureSession.commitConfiguration() UIView.animateWithDuration(1.3, animations: { [unowned self] in self.containerView.alpha = 0 @@ -129,7 +132,10 @@ class CameraView: UIViewController { func flashCamera(title: String) { if (captureDevice?.hasFlash != nil) { - captureDevice?.lockForConfiguration(nil) + do { + try captureDevice?.lockForConfiguration() + } catch _ { + } switch title { case "ON": captureDevice?.flashMode = .On @@ -173,7 +179,7 @@ class CameraView: UIViewController { func focusTo(point: CGPoint) { if let device = captureDevice { - if device.lockForConfiguration(nil) + if device.lockForConfiguration() && device.isFocusModeSupported(AVCaptureFocusMode.Locked) { device.focusPointOfInterest = CGPointMake(point.x / UIScreen.mainScreen().bounds.width, point.y / UIScreen.mainScreen().bounds.height) device.unlockForConfiguration() @@ -189,8 +195,8 @@ class CameraView: UIViewController { } } - override func touchesBegan(touches: Set, withEvent event: UIEvent) { - let anyTouch = touches.first as! UITouch + override func touchesBegan(touches: Set, withEvent event: UIEvent?) { + let anyTouch = touches.first! let touchX = anyTouch.locationInView(view).x let touchY = anyTouch.locationInView(view).y focusImageView.transform = CGAffineTransformIdentity @@ -200,26 +206,32 @@ class CameraView: UIViewController { func configureDevice() { if let device = captureDevice { - device.lockForConfiguration(nil) + do { + try device.lockForConfiguration() + } catch _ { + } device.unlockForConfiguration() } } func beginSession() { configureDevice() - var error: NSError? = nil + let error: NSError? = nil if captureSession.inputs.count == 0 { - captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error)) + let captureDeviceInput: AVCaptureDeviceInput? + do { try captureDeviceInput = AVCaptureDeviceInput(device: self.captureDevice) + } catch { } + captureSession.addInput(captureDeviceInput) if error != nil { - println("error: \(error?.localizedDescription)") + print("error: \(error?.localizedDescription)") } previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) previewLayer?.autoreverses = true previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill view.clipsToBounds = true - view.layer.addSublayer(previewLayer) + view.layer.addSublayer(previewLayer!) previewLayer?.frame = view.layer.frame captureSession.startRunning() delegate?.handleFlashButton(captureDevice?.position == .Front) diff --git a/Source/ImageGallery/ImageGalleryView.swift b/Source/ImageGallery/ImageGalleryView.swift index b96c74da..dc8d690b 100644 --- a/Source/ImageGallery/ImageGalleryView.swift +++ b/Source/ImageGallery/ImageGalleryView.swift @@ -25,7 +25,7 @@ public class ImageGalleryView: UIView { lazy public var collectionView: UICollectionView = { [unowned self] in let collectionView = UICollectionView(frame: CGRectMake(0, 0, 0, 0), collectionViewLayout: self.collectionViewLayout) - collectionView.setTranslatesAutoresizingMaskIntoConstraints(false) + collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.backgroundColor = self.configuration.mainColor collectionView.showsHorizontalScrollIndicator = false collectionView.layer.anchorPoint = CGPointMake(0.5, 0.5) @@ -45,7 +45,7 @@ public class ImageGalleryView: UIView { lazy var topSeparator: UIView = { [unowned self] in let view = UIView() - view.setTranslatesAutoresizingMaskIntoConstraints(false) + view.translatesAutoresizingMaskIntoConstraints = false view.addGestureRecognizer(self.panGestureRecognizer) view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6) @@ -56,7 +56,7 @@ public class ImageGalleryView: UIView { let view = UIView() view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.6) view.layer.cornerRadius = Dimensions.indicatorHeight / 2 - view.setTranslatesAutoresizingMaskIntoConstraints(false) + view.translatesAutoresizingMaskIntoConstraints = false return view }() @@ -112,7 +112,7 @@ public class ImageGalleryView: UIView { fetchPhotos(0) } - required public init(coder aDecoder: NSCoder) { + required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/Source/ImageGallery/ImageGalleryViewCell.swift b/Source/ImageGallery/ImageGalleryViewCell.swift index af7adff1..3c887b94 100644 --- a/Source/ImageGallery/ImageGalleryViewCell.swift +++ b/Source/ImageGallery/ImageGalleryViewCell.swift @@ -22,7 +22,7 @@ class ImageGalleryViewCell: UICollectionViewCell { if imageView.superview != contentView { for view in [imageView, selectedImageView] { view.contentMode = .ScaleAspectFill - view.setTranslatesAutoresizingMaskIntoConstraints(false) + view.translatesAutoresizingMaskIntoConstraints = false view.clipsToBounds = true contentView.addSubview(view) } diff --git a/Source/ImagePickerController.swift b/Source/ImagePickerController.swift index a845d097..cbf53f16 100644 --- a/Source/ImagePickerController.swift +++ b/Source/ImagePickerController.swift @@ -77,7 +77,7 @@ public class ImagePickerController: UIViewController { for subview in [cameraController.view, galleryView, bottomContainer, topView] { view.addSubview(subview) - subview.setTranslatesAutoresizingMaskIntoConstraints(false) + subview.translatesAutoresizingMaskIntoConstraints = false } view.backgroundColor = .whiteColor() @@ -293,7 +293,7 @@ extension ImagePickerController: ImageGalleryPanGestureDelegate { galleryView.collectionSize = CGSize(width: galleryView.collectionView.frame.height, height: galleryView.collectionView.frame.height) if galleryHeight < GestureConstants.maximumHeight { - var realTranslation = translation.y < -GestureConstants.minimumHeight + ImageGalleryView.Dimensions.galleryBarHeight + let realTranslation = translation.y < -GestureConstants.minimumHeight + ImageGalleryView.Dimensions.galleryBarHeight ? translation.y + GestureConstants.minimumHeight - ImageGalleryView.Dimensions.galleryBarHeight : translation.y galleryView.collectionView.contentOffset = CGPoint(x: initialContentOffset.x - (realTranslation * CGFloat(numberOfCells)), y: 0) diff --git a/Source/TopView/TopView.swift b/Source/TopView/TopView.swift index 0b9bbef7..0140fc7b 100644 --- a/Source/TopView/TopView.swift +++ b/Source/TopView/TopView.swift @@ -53,14 +53,14 @@ class TopView: UIView { button.layer.shadowOpacity = 0.5 button.layer.shadowOffset = CGSize(width: 0, height: 1) button.layer.shadowRadius = 1 - button.setTranslatesAutoresizingMaskIntoConstraints(false) + button.translatesAutoresizingMaskIntoConstraints = false addSubview(button) } setupConstraints() } - required init(coder aDecoder: NSCoder) { + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }