/CameraButton

A simple camera button that can be used for photo and video capturing.

Primary LanguageSwiftMIT LicenseMIT

CameraButton

RPReplay_Final1642669292 RPReplay_Final1642670033

A simple camera button that can be used for photo and video capturing.

Requirements

iOS 14.0 or higher

Instalation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/erikdrobne/CameraButton")
]

Usage

Import

import CameraButton

UIKit

Initialize

let button = CameraButton()
button.delegate = self
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
    button.widthAnchor.constraint(equalToConstant: 72),
    button.heightAnchor.constraint(equalToConstant: 72),
    button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -64)
])

Customize

// Set custom colors
button.borderColor = .red
button.fillColor = (.purple, .orange)
button.progressColor = .green

// Set progress animation duration
button.progressDuration = 5

// Start progress animation
button.start()

// Stop progress animation
button.stop()

Delegate

The CameraButtonDelegate requires you to implement the following methods:

func didTap(_ button: CameraButton)
func didFinishProgress()

SwiftUI

struct PhotoView: View {

    @State var isRecording: Bool = false
    @State var didFinishProgress: Bool = false

    var body: some View {
        CameraButtonUI(
            size: 72,
            borderColor: .red,
            fillColor: (.purple, .orange),
            progressColor: .green,
            progressDuration: 5,
            isRecording: self.$isRecording
        )
        // Handle tap gesture
        .simultaneousGesture(
            TapGesture()
                .onEnded { _ in
                    print("tap")
                }
        )
        // Start recording on Long-press gesture
        .gesture(
            LongPressGesture(minimumDuration: 1)
                .onChanged { val in
                    isRecording = true
                }
        )
        // Observe state changes
        .onChange(of: isRecording, perform: { [isRecording] newValue in
            print("isRecording", isRecording, newValue)
        })
    }
}