Skip to content

Commit

Permalink
Capture action changes
Browse files Browse the repository at this point in the history
Example project
  • Loading branch information
David Jonsén committed Sep 1, 2018
1 parent b4dc2e3 commit cea1761
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DJCaptureButton.podspec
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'DJCaptureButton'
s.version = '0.1.0'
s.version = '0.1.1'
s.summary = 'Camera styled capture button with 3D touch'

# This description is used to generate tags and improve search results.
Expand Down
46 changes: 30 additions & 16 deletions DJCaptureButton/Classes/DJCaptureButton.swift
Expand Up @@ -49,10 +49,10 @@ open class DJCaptureButton: UIButton {

public var generateFeedback: Bool = true

internal lazy var impactFeedbackGenerator: UIImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)

// MARK: - Internal

internal lazy var impactFeedbackGenerator: UIImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)

internal var forceTouchGestureRecognizer: ForceTouchGestureRecognizer?

internal lazy var tapGestureRecognizer: UITapGestureRecognizer = {
Expand Down Expand Up @@ -104,6 +104,8 @@ open class DJCaptureButton: UIButton {
}
}

// MARK: - Setup

extension DJCaptureButton {

internal func setupButton() {
Expand Down Expand Up @@ -137,25 +139,21 @@ extension DJCaptureButton {
let forceGesture = ForceTouchGestureRecognizer(target: self, action: #selector(handleForce))
forceGesture.delegate = self
addGestureRecognizer(forceGesture)
forceTouchGestureRecognizer = forceGesture
}

internal func updateMiddleCircle() {

let offset = borderWidth + middleCircleOffset

middleCircle.path = UIBezierPath(ovalIn: CGRect(x: offset, y: offset, width: bounds.width - (offset * 2), height: bounds.height - (offset * 2))).cgPath
forceTouchGestureRecognizer = forceGesture
}
}

// MARK: -

extension DJCaptureButton {

@objc
internal func handleTap(gesture: ForceTouchGestureRecognizer) {
internal func handleTap(gesture: UITapGestureRecognizer) {

animateTapGesture()

fireButton()
captureButtonFireAction()
}

@objc
Expand All @@ -165,12 +163,22 @@ extension DJCaptureButton {

case .ended, .cancelled:

if tapGestureRecognizer.isEnabled == false {
captureButtonFireAction()
}

gesture.isEnabled = true

resetMiddleCircle(delay: 100)

tapGestureRecognizer.isEnabled = true

case .changed:

if tapGestureRecognizer.isEnabled {
tapGestureRecognizer.isEnabled = false
}

let scale = 1 - min(gesture.force, 0.2)

middleCircle.transform = CATransform3DMakeScale(scale, scale, 1.0)
Expand All @@ -179,12 +187,9 @@ extension DJCaptureButton {
return
}

tapGestureRecognizer.isEnabled = false

gesture.isEnabled = false
gesture.isEnabled = true

fireButton()
// fireButton()

default:
break
Expand All @@ -194,14 +199,21 @@ extension DJCaptureButton {

extension DJCaptureButton {

internal func updateMiddleCircle() {

let offset = borderWidth + middleCircleOffset

middleCircle.path = UIBezierPath(ovalIn: CGRect(x: offset, y: offset, width: bounds.width - (offset * 2), height: bounds.height - (offset * 2))).cgPath
}

internal func resetMiddleCircle(delay milliseconds: Int = 0) {

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(milliseconds)) {
self.middleCircle.transform = CATransform3DMakeScale(1.0, 1.0, 1.0)
}
}

internal func fireButton() {
internal func captureButtonFireAction() {

if generateFeedback {
impactFeedbackGenerator.impactOccurred()
Expand All @@ -221,6 +233,8 @@ extension DJCaptureButton {
}
}

// MARK: - UIGestureRecognizerDelegate

extension DJCaptureButton: UIGestureRecognizerDelegate {

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
Expand Down
10 changes: 5 additions & 5 deletions Example/DJCaptureButton/Base.lproj/Main.storyboard
Expand Up @@ -32,12 +32,12 @@
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="📷" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="KjR-Ch-BxZ">
<rect key="frame" x="108.5" y="28" width="158" height="125"/>
<rect key="frame" x="90" y="200" width="195" height="115"/>
<constraints>
<constraint firstAttribute="width" constant="158" id="2pJ-qv-aLk"/>
<constraint firstAttribute="height" constant="125" id="NYv-kz-SfN"/>
<constraint firstAttribute="width" constant="195" id="2pJ-qv-aLk"/>
<constraint firstAttribute="height" constant="115" id="NYv-kz-SfN"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="120"/>
<fontDescription key="fontDescription" type="system" pointSize="80"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
Expand All @@ -47,7 +47,7 @@
<constraint firstItem="geG-v9-ihz" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="DYN-Om-Co6"/>
<constraint firstItem="KjR-Ch-BxZ" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="Ugx-e7-8Iy"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="geG-v9-ihz" secondAttribute="bottom" constant="45" id="goZ-wu-pcf"/>
<constraint firstItem="KjR-Ch-BxZ" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="8" id="lgM-BN-oF6"/>
<constraint firstItem="KjR-Ch-BxZ" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="180" id="lgM-BN-oF6"/>
</constraints>
</view>
<connections>
Expand Down

0 comments on commit cea1761

Please sign in to comment.