diff --git a/DemoSwiftyCam/DemoSwiftyCam.xcodeproj/project.pbxproj b/DemoSwiftyCam/DemoSwiftyCam.xcodeproj/project.pbxproj index c34f98e..2a84cf7 100644 --- a/DemoSwiftyCam/DemoSwiftyCam.xcodeproj/project.pbxproj +++ b/DemoSwiftyCam/DemoSwiftyCam.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 16298B561E2703DC0056D413 /* SwiftyRecordButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16298B551E2703DC0056D413 /* SwiftyRecordButton.swift */; }; 1675A9761E00A68300B80903 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1675A9751E00A68300B80903 /* AppDelegate.swift */; }; 1675A9781E00A68300B80903 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1675A9771E00A68300B80903 /* ViewController.swift */; }; 1675A97B1E00A68300B80903 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1675A9791E00A68300B80903 /* Main.storyboard */; }; @@ -19,6 +20,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 16298B551E2703DC0056D413 /* SwiftyRecordButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyRecordButton.swift; sourceTree = ""; }; 1675A9721E00A68300B80903 /* DemoSwiftyCam.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoSwiftyCam.app; sourceTree = BUILT_PRODUCTS_DIR; }; 1675A9751E00A68300B80903 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 1675A9771E00A68300B80903 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -65,6 +67,7 @@ 1675A9911E00A74F00B80903 /* Source */, 1675A9751E00A68300B80903 /* AppDelegate.swift */, 1675A9771E00A68300B80903 /* ViewController.swift */, + 16298B551E2703DC0056D413 /* SwiftyRecordButton.swift */, 1675A9791E00A68300B80903 /* Main.storyboard */, 1675A97C1E00A68300B80903 /* Assets.xcassets */, 1675A97E1E00A68300B80903 /* LaunchScreen.storyboard */, @@ -161,6 +164,7 @@ 1675A9901E00A74A00B80903 /* SwiftyCamViewControllerDelegate.swift in Sources */, 1675A9761E00A68300B80903 /* AppDelegate.swift in Sources */, 1675A98F1E00A74A00B80903 /* SwiftyCamViewController.swift in Sources */, + 16298B561E2703DC0056D413 /* SwiftyRecordButton.swift in Sources */, 1675A98D1E00A74A00B80903 /* PreviewView.swift in Sources */, 1675A98E1E00A74A00B80903 /* SwiftyCamButton.swift in Sources */, ); diff --git a/DemoSwiftyCam/DemoSwiftyCam/Base.lproj/Main.storyboard b/DemoSwiftyCam/DemoSwiftyCam/Base.lproj/Main.storyboard index eb8ac80..b199a4c 100644 --- a/DemoSwiftyCam/DemoSwiftyCam/Base.lproj/Main.storyboard +++ b/DemoSwiftyCam/DemoSwiftyCam/Base.lproj/Main.storyboard @@ -35,27 +35,16 @@ - - - - @@ -65,7 +54,6 @@ - diff --git a/DemoSwiftyCam/DemoSwiftyCam/SwiftyRecordButton.swift b/DemoSwiftyCam/DemoSwiftyCam/SwiftyRecordButton.swift new file mode 100644 index 0000000..4f48c4b --- /dev/null +++ b/DemoSwiftyCam/DemoSwiftyCam/SwiftyRecordButton.swift @@ -0,0 +1,102 @@ +// +// SwiftyRecordButton.swift +// SwiftyRecordButton +// +// Created by Andrew on 2017-01-11. +// Copyright © 2017 Walzy. All rights reserved. +// + +import UIKit + +class SwiftyRecordButton: SwiftyCamButton, UIGestureRecognizerDelegate { + + private var circleBorder: CALayer! + private var innerCircle: UIView! + + override init(frame: CGRect) { + super.init(frame: frame) + drawButton() + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + drawButton() + } + + private func drawButton() { + self.backgroundColor = UIColor.clear + + circleBorder = CALayer() + circleBorder.backgroundColor = UIColor.clear.cgColor + circleBorder.borderWidth = 6.0 + circleBorder.borderColor = UIColor.white.cgColor + circleBorder.bounds = self.bounds + circleBorder.position = CGPoint(x: self.bounds.midX, y: self.bounds.midY) + circleBorder.cornerRadius = self.frame.size.width / 2 + layer.insertSublayer(circleBorder, at: 0) + + let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesture:))) + longPress.delegate = self + self.addGestureRecognizer(longPress) + } + + func handleLongPress(gesture: UILongPressGestureRecognizer){ + switch gesture.state { + case .began: + growButton() + case .ended: + shrinkButton() + default: + break + } + } + + public func growButton() { + let scale = CABasicAnimation(keyPath: "transform.scale") + scale.fromValue = 1.0 + scale.toValue = 1.20 + scale.duration = 0.3 + scale.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) + scale.fillMode = kCAFillModeForwards + scale.isRemovedOnCompletion = false + + + innerCircle = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1)) + innerCircle.center = CGPoint(x: self.bounds.midX, y: self.bounds.midY) + innerCircle.backgroundColor = UIColor.red + innerCircle.layer.cornerRadius = innerCircle.frame.size.width / 2 + innerCircle.clipsToBounds = true + self.addSubview(innerCircle) + + UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: { + self.innerCircle.transform = CGAffineTransform(scaleX: 50.0, y: 50.0) + + }, completion: nil) + + circleBorder.add(scale, forKey: "grow") + } + + public func shrinkButton() { + let scale = CABasicAnimation(keyPath: "transform.scale") + scale.fromValue = 1.20 + scale.toValue = 1.0 + scale.duration = 0.3 + scale.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) + scale.fillMode = kCAFillModeForwards + scale.isRemovedOnCompletion = false + + UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: { + self.innerCircle.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) + + }, completion: { (success) in + self.innerCircle.removeFromSuperview() + self.innerCircle = nil + }) + + circleBorder.add(scale, forKey: "grow") + } + + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return true + } +} diff --git a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift index 9722cb2..78ec1a1 100644 --- a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift +++ b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift @@ -20,7 +20,7 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate { @IBOutlet weak var flipCameraButton: UIButton! @IBOutlet weak var toggleFlashButton: UIButton! - @IBOutlet weak var captureButton: SwiftyCamButton! + var captureButton: SwiftyRecordButton! override func viewDidLoad() { super.viewDidLoad() @@ -34,6 +34,9 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) + + captureButton = SwiftyRecordButton(frame: CGRect(x: view.frame.midX - 37.5, y: view.frame.height - 100.0, width: 75.0, height: 75.0)) + self.view.addSubview(captureButton) captureButton.delegate = self self.view.bringSubview(toFront: captureButton) self.view.bringSubview(toFront: flipCameraButton)