├── .gitignore
├── .swiftpm
└── xcode
│ ├── package.xcworkspace
│ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcshareddata
│ └── xcschemes
│ └── CameraButtonUI.xcscheme
├── CameraButtonExample
├── CameraButtonExample.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
└── CameraButtonExample
│ ├── AppDelegate.swift
│ ├── Assets.xcassets
│ ├── AccentColor.colorset
│ │ └── Contents.json
│ ├── AppIcon.appiconset
│ │ └── Contents.json
│ └── Contents.json
│ ├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
│ ├── Info.plist
│ ├── PhotoView.swift
│ ├── SceneDelegate.swift
│ └── ViewController.swift
├── LICENSE
├── Package.swift
├── README.md
├── Sources
├── CameraButton
│ └── CameraButton.swift
└── CameraButtonUI
│ ├── CameraButtonUI.swift
│ └── ReversingScale.swift
└── Tests
└── CameraButtonTests
└── CameraButtonTests.swift
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 | xcuserdata/
6 | DerivedData/
7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/xcshareddata/xcschemes/CameraButtonUI.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
32 |
33 |
43 |
44 |
50 |
51 |
57 |
58 |
59 |
60 |
62 |
63 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 56;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 17DD374928F859AE00BF29E7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DD374828F859AE00BF29E7 /* AppDelegate.swift */; };
11 | 17DD374B28F859AE00BF29E7 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DD374A28F859AE00BF29E7 /* SceneDelegate.swift */; };
12 | 17DD374D28F859AE00BF29E7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DD374C28F859AE00BF29E7 /* ViewController.swift */; };
13 | 17DD375028F859AE00BF29E7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 17DD374E28F859AE00BF29E7 /* Main.storyboard */; };
14 | 17DD375228F859B000BF29E7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 17DD375128F859B000BF29E7 /* Assets.xcassets */; };
15 | 17DD375528F859B000BF29E7 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 17DD375328F859B000BF29E7 /* LaunchScreen.storyboard */; };
16 | 17DD375E28F859E700BF29E7 /* CameraButton in Frameworks */ = {isa = PBXBuildFile; productRef = 17DD375D28F859E700BF29E7 /* CameraButton */; };
17 | 17F38B70293A275A00793D23 /* PhotoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17F38B6F293A275A00793D23 /* PhotoView.swift */; };
18 | /* End PBXBuildFile section */
19 |
20 | /* Begin PBXFileReference section */
21 | 174DA71D28F97ED40044D8BA /* CameraButton */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = CameraButton; path = ..; sourceTree = ""; };
22 | 17DD374528F859AE00BF29E7 /* CameraButtonExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CameraButtonExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
23 | 17DD374828F859AE00BF29E7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
24 | 17DD374A28F859AE00BF29E7 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; };
25 | 17DD374C28F859AE00BF29E7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
26 | 17DD374F28F859AE00BF29E7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
27 | 17DD375128F859B000BF29E7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
28 | 17DD375428F859B000BF29E7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
29 | 17DD375628F859B000BF29E7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
30 | 17F38B6F293A275A00793D23 /* PhotoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhotoView.swift; sourceTree = ""; };
31 | /* End PBXFileReference section */
32 |
33 | /* Begin PBXFrameworksBuildPhase section */
34 | 17DD374228F859AE00BF29E7 /* Frameworks */ = {
35 | isa = PBXFrameworksBuildPhase;
36 | buildActionMask = 2147483647;
37 | files = (
38 | 17DD375E28F859E700BF29E7 /* CameraButton in Frameworks */,
39 | );
40 | runOnlyForDeploymentPostprocessing = 0;
41 | };
42 | /* End PBXFrameworksBuildPhase section */
43 |
44 | /* Begin PBXGroup section */
45 | 17DD373C28F859AE00BF29E7 = {
46 | isa = PBXGroup;
47 | children = (
48 | 174DA71D28F97ED40044D8BA /* CameraButton */,
49 | 17DD374728F859AE00BF29E7 /* CameraButtonExample */,
50 | 17DD374628F859AE00BF29E7 /* Products */,
51 | 17DD375F28F85BCE00BF29E7 /* Frameworks */,
52 | );
53 | sourceTree = "";
54 | };
55 | 17DD374628F859AE00BF29E7 /* Products */ = {
56 | isa = PBXGroup;
57 | children = (
58 | 17DD374528F859AE00BF29E7 /* CameraButtonExample.app */,
59 | );
60 | name = Products;
61 | sourceTree = "";
62 | };
63 | 17DD374728F859AE00BF29E7 /* CameraButtonExample */ = {
64 | isa = PBXGroup;
65 | children = (
66 | 17DD374828F859AE00BF29E7 /* AppDelegate.swift */,
67 | 17DD374A28F859AE00BF29E7 /* SceneDelegate.swift */,
68 | 17DD374C28F859AE00BF29E7 /* ViewController.swift */,
69 | 17DD374E28F859AE00BF29E7 /* Main.storyboard */,
70 | 17DD375128F859B000BF29E7 /* Assets.xcassets */,
71 | 17DD375328F859B000BF29E7 /* LaunchScreen.storyboard */,
72 | 17DD375628F859B000BF29E7 /* Info.plist */,
73 | 17F38B6F293A275A00793D23 /* PhotoView.swift */,
74 | );
75 | path = CameraButtonExample;
76 | sourceTree = "";
77 | };
78 | 17DD375F28F85BCE00BF29E7 /* Frameworks */ = {
79 | isa = PBXGroup;
80 | children = (
81 | );
82 | name = Frameworks;
83 | sourceTree = "";
84 | };
85 | /* End PBXGroup section */
86 |
87 | /* Begin PBXNativeTarget section */
88 | 17DD374428F859AE00BF29E7 /* CameraButtonExample */ = {
89 | isa = PBXNativeTarget;
90 | buildConfigurationList = 17DD375928F859B000BF29E7 /* Build configuration list for PBXNativeTarget "CameraButtonExample" */;
91 | buildPhases = (
92 | 17DD374128F859AE00BF29E7 /* Sources */,
93 | 17DD374228F859AE00BF29E7 /* Frameworks */,
94 | 17DD374328F859AE00BF29E7 /* Resources */,
95 | );
96 | buildRules = (
97 | );
98 | dependencies = (
99 | );
100 | name = CameraButtonExample;
101 | packageProductDependencies = (
102 | 17DD375D28F859E700BF29E7 /* CameraButton */,
103 | );
104 | productName = CameraButtonExample;
105 | productReference = 17DD374528F859AE00BF29E7 /* CameraButtonExample.app */;
106 | productType = "com.apple.product-type.application";
107 | };
108 | /* End PBXNativeTarget section */
109 |
110 | /* Begin PBXProject section */
111 | 17DD373D28F859AE00BF29E7 /* Project object */ = {
112 | isa = PBXProject;
113 | attributes = {
114 | BuildIndependentTargetsInParallel = 1;
115 | LastSwiftUpdateCheck = 1400;
116 | LastUpgradeCheck = 1400;
117 | TargetAttributes = {
118 | 17DD374428F859AE00BF29E7 = {
119 | CreatedOnToolsVersion = 14.0.1;
120 | };
121 | };
122 | };
123 | buildConfigurationList = 17DD374028F859AE00BF29E7 /* Build configuration list for PBXProject "CameraButtonExample" */;
124 | compatibilityVersion = "Xcode 14.0";
125 | developmentRegion = en;
126 | hasScannedForEncodings = 0;
127 | knownRegions = (
128 | en,
129 | Base,
130 | );
131 | mainGroup = 17DD373C28F859AE00BF29E7;
132 | packageReferences = (
133 | 17DD375C28F859E700BF29E7 /* XCRemoteSwiftPackageReference "CameraButton" */,
134 | );
135 | productRefGroup = 17DD374628F859AE00BF29E7 /* Products */;
136 | projectDirPath = "";
137 | projectRoot = "";
138 | targets = (
139 | 17DD374428F859AE00BF29E7 /* CameraButtonExample */,
140 | );
141 | };
142 | /* End PBXProject section */
143 |
144 | /* Begin PBXResourcesBuildPhase section */
145 | 17DD374328F859AE00BF29E7 /* Resources */ = {
146 | isa = PBXResourcesBuildPhase;
147 | buildActionMask = 2147483647;
148 | files = (
149 | 17DD375528F859B000BF29E7 /* LaunchScreen.storyboard in Resources */,
150 | 17DD375228F859B000BF29E7 /* Assets.xcassets in Resources */,
151 | 17DD375028F859AE00BF29E7 /* Main.storyboard in Resources */,
152 | );
153 | runOnlyForDeploymentPostprocessing = 0;
154 | };
155 | /* End PBXResourcesBuildPhase section */
156 |
157 | /* Begin PBXSourcesBuildPhase section */
158 | 17DD374128F859AE00BF29E7 /* Sources */ = {
159 | isa = PBXSourcesBuildPhase;
160 | buildActionMask = 2147483647;
161 | files = (
162 | 17DD374D28F859AE00BF29E7 /* ViewController.swift in Sources */,
163 | 17DD374928F859AE00BF29E7 /* AppDelegate.swift in Sources */,
164 | 17DD374B28F859AE00BF29E7 /* SceneDelegate.swift in Sources */,
165 | 17F38B70293A275A00793D23 /* PhotoView.swift in Sources */,
166 | );
167 | runOnlyForDeploymentPostprocessing = 0;
168 | };
169 | /* End PBXSourcesBuildPhase section */
170 |
171 | /* Begin PBXVariantGroup section */
172 | 17DD374E28F859AE00BF29E7 /* Main.storyboard */ = {
173 | isa = PBXVariantGroup;
174 | children = (
175 | 17DD374F28F859AE00BF29E7 /* Base */,
176 | );
177 | name = Main.storyboard;
178 | sourceTree = "";
179 | };
180 | 17DD375328F859B000BF29E7 /* LaunchScreen.storyboard */ = {
181 | isa = PBXVariantGroup;
182 | children = (
183 | 17DD375428F859B000BF29E7 /* Base */,
184 | );
185 | name = LaunchScreen.storyboard;
186 | sourceTree = "";
187 | };
188 | /* End PBXVariantGroup section */
189 |
190 | /* Begin XCBuildConfiguration section */
191 | 17DD375728F859B000BF29E7 /* Debug */ = {
192 | isa = XCBuildConfiguration;
193 | buildSettings = {
194 | ALWAYS_SEARCH_USER_PATHS = NO;
195 | CLANG_ANALYZER_NONNULL = YES;
196 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
197 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
198 | CLANG_ENABLE_MODULES = YES;
199 | CLANG_ENABLE_OBJC_ARC = YES;
200 | CLANG_ENABLE_OBJC_WEAK = YES;
201 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
202 | CLANG_WARN_BOOL_CONVERSION = YES;
203 | CLANG_WARN_COMMA = YES;
204 | CLANG_WARN_CONSTANT_CONVERSION = YES;
205 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
206 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
207 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
208 | CLANG_WARN_EMPTY_BODY = YES;
209 | CLANG_WARN_ENUM_CONVERSION = YES;
210 | CLANG_WARN_INFINITE_RECURSION = YES;
211 | CLANG_WARN_INT_CONVERSION = YES;
212 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
213 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
214 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
215 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
216 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
217 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
218 | CLANG_WARN_STRICT_PROTOTYPES = YES;
219 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
220 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
221 | CLANG_WARN_UNREACHABLE_CODE = YES;
222 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
223 | COPY_PHASE_STRIP = NO;
224 | DEBUG_INFORMATION_FORMAT = dwarf;
225 | ENABLE_STRICT_OBJC_MSGSEND = YES;
226 | ENABLE_TESTABILITY = YES;
227 | GCC_C_LANGUAGE_STANDARD = gnu11;
228 | GCC_DYNAMIC_NO_PIC = NO;
229 | GCC_NO_COMMON_BLOCKS = YES;
230 | GCC_OPTIMIZATION_LEVEL = 0;
231 | GCC_PREPROCESSOR_DEFINITIONS = (
232 | "DEBUG=1",
233 | "$(inherited)",
234 | );
235 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
236 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
237 | GCC_WARN_UNDECLARED_SELECTOR = YES;
238 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
239 | GCC_WARN_UNUSED_FUNCTION = YES;
240 | GCC_WARN_UNUSED_VARIABLE = YES;
241 | IPHONEOS_DEPLOYMENT_TARGET = 16.0;
242 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
243 | MTL_FAST_MATH = YES;
244 | ONLY_ACTIVE_ARCH = YES;
245 | SDKROOT = iphoneos;
246 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
247 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
248 | };
249 | name = Debug;
250 | };
251 | 17DD375828F859B000BF29E7 /* Release */ = {
252 | isa = XCBuildConfiguration;
253 | buildSettings = {
254 | ALWAYS_SEARCH_USER_PATHS = NO;
255 | CLANG_ANALYZER_NONNULL = YES;
256 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
257 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
258 | CLANG_ENABLE_MODULES = YES;
259 | CLANG_ENABLE_OBJC_ARC = YES;
260 | CLANG_ENABLE_OBJC_WEAK = YES;
261 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
262 | CLANG_WARN_BOOL_CONVERSION = YES;
263 | CLANG_WARN_COMMA = YES;
264 | CLANG_WARN_CONSTANT_CONVERSION = YES;
265 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
266 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
267 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
268 | CLANG_WARN_EMPTY_BODY = YES;
269 | CLANG_WARN_ENUM_CONVERSION = YES;
270 | CLANG_WARN_INFINITE_RECURSION = YES;
271 | CLANG_WARN_INT_CONVERSION = YES;
272 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
273 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
274 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
275 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
276 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
277 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
278 | CLANG_WARN_STRICT_PROTOTYPES = YES;
279 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
280 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
281 | CLANG_WARN_UNREACHABLE_CODE = YES;
282 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
283 | COPY_PHASE_STRIP = NO;
284 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
285 | ENABLE_NS_ASSERTIONS = NO;
286 | ENABLE_STRICT_OBJC_MSGSEND = YES;
287 | GCC_C_LANGUAGE_STANDARD = gnu11;
288 | GCC_NO_COMMON_BLOCKS = YES;
289 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
290 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
291 | GCC_WARN_UNDECLARED_SELECTOR = YES;
292 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
293 | GCC_WARN_UNUSED_FUNCTION = YES;
294 | GCC_WARN_UNUSED_VARIABLE = YES;
295 | IPHONEOS_DEPLOYMENT_TARGET = 16.0;
296 | MTL_ENABLE_DEBUG_INFO = NO;
297 | MTL_FAST_MATH = YES;
298 | SDKROOT = iphoneos;
299 | SWIFT_COMPILATION_MODE = wholemodule;
300 | SWIFT_OPTIMIZATION_LEVEL = "-O";
301 | VALIDATE_PRODUCT = YES;
302 | };
303 | name = Release;
304 | };
305 | 17DD375A28F859B000BF29E7 /* Debug */ = {
306 | isa = XCBuildConfiguration;
307 | buildSettings = {
308 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
309 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
310 | CODE_SIGN_STYLE = Automatic;
311 | CURRENT_PROJECT_VERSION = 1;
312 | DEVELOPMENT_TEAM = R69RCQLH2U;
313 | GENERATE_INFOPLIST_FILE = YES;
314 | INFOPLIST_FILE = CameraButtonExample/Info.plist;
315 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
316 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
317 | INFOPLIST_KEY_UIMainStoryboardFile = Main;
318 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
319 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
320 | LD_RUNPATH_SEARCH_PATHS = (
321 | "$(inherited)",
322 | "@executable_path/Frameworks",
323 | );
324 | MARKETING_VERSION = 1.0;
325 | PRODUCT_BUNDLE_IDENTIFIER = com.CameraButtonExample;
326 | PRODUCT_NAME = "$(TARGET_NAME)";
327 | SWIFT_EMIT_LOC_STRINGS = YES;
328 | SWIFT_VERSION = 5.0;
329 | TARGETED_DEVICE_FAMILY = "1,2";
330 | };
331 | name = Debug;
332 | };
333 | 17DD375B28F859B000BF29E7 /* Release */ = {
334 | isa = XCBuildConfiguration;
335 | buildSettings = {
336 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
337 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
338 | CODE_SIGN_STYLE = Automatic;
339 | CURRENT_PROJECT_VERSION = 1;
340 | DEVELOPMENT_TEAM = R69RCQLH2U;
341 | GENERATE_INFOPLIST_FILE = YES;
342 | INFOPLIST_FILE = CameraButtonExample/Info.plist;
343 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
344 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
345 | INFOPLIST_KEY_UIMainStoryboardFile = Main;
346 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
347 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
348 | LD_RUNPATH_SEARCH_PATHS = (
349 | "$(inherited)",
350 | "@executable_path/Frameworks",
351 | );
352 | MARKETING_VERSION = 1.0;
353 | PRODUCT_BUNDLE_IDENTIFIER = com.CameraButtonExample;
354 | PRODUCT_NAME = "$(TARGET_NAME)";
355 | SWIFT_EMIT_LOC_STRINGS = YES;
356 | SWIFT_VERSION = 5.0;
357 | TARGETED_DEVICE_FAMILY = "1,2";
358 | };
359 | name = Release;
360 | };
361 | /* End XCBuildConfiguration section */
362 |
363 | /* Begin XCConfigurationList section */
364 | 17DD374028F859AE00BF29E7 /* Build configuration list for PBXProject "CameraButtonExample" */ = {
365 | isa = XCConfigurationList;
366 | buildConfigurations = (
367 | 17DD375728F859B000BF29E7 /* Debug */,
368 | 17DD375828F859B000BF29E7 /* Release */,
369 | );
370 | defaultConfigurationIsVisible = 0;
371 | defaultConfigurationName = Release;
372 | };
373 | 17DD375928F859B000BF29E7 /* Build configuration list for PBXNativeTarget "CameraButtonExample" */ = {
374 | isa = XCConfigurationList;
375 | buildConfigurations = (
376 | 17DD375A28F859B000BF29E7 /* Debug */,
377 | 17DD375B28F859B000BF29E7 /* Release */,
378 | );
379 | defaultConfigurationIsVisible = 0;
380 | defaultConfigurationName = Release;
381 | };
382 | /* End XCConfigurationList section */
383 |
384 | /* Begin XCRemoteSwiftPackageReference section */
385 | 17DD375C28F859E700BF29E7 /* XCRemoteSwiftPackageReference "CameraButton" */ = {
386 | isa = XCRemoteSwiftPackageReference;
387 | repositoryURL = "https://github.com/erikdrobne/CameraButton";
388 | requirement = {
389 | branch = main;
390 | kind = branch;
391 | };
392 | };
393 | /* End XCRemoteSwiftPackageReference section */
394 |
395 | /* Begin XCSwiftPackageProductDependency section */
396 | 17DD375D28F859E700BF29E7 /* CameraButton */ = {
397 | isa = XCSwiftPackageProductDependency;
398 | package = 17DD375C28F859E700BF29E7 /* XCRemoteSwiftPackageReference "CameraButton" */;
399 | productName = CameraButton;
400 | };
401 | /* End XCSwiftPackageProductDependency section */
402 | };
403 | rootObject = 17DD373D28F859AE00BF29E7 /* Project object */;
404 | }
405 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // CameraButtonExample
4 | //
5 | // Created by Erik Drobne on 13/10/2022.
6 | //
7 |
8 | import UIKit
9 |
10 | @main
11 | class AppDelegate: UIResponder, UIApplicationDelegate {
12 |
13 |
14 |
15 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
16 | // Override point for customization after application launch.
17 | return true
18 | }
19 |
20 | // MARK: UISceneSession Lifecycle
21 |
22 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
23 | // Called when a new scene session is being created.
24 | // Use this method to select a configuration to create the new scene with.
25 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
26 | }
27 |
28 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) {
29 | // Called when the user discards a scene session.
30 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
31 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
32 | }
33 |
34 |
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/Assets.xcassets/AccentColor.colorset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "colors" : [
3 | {
4 | "idiom" : "universal"
5 | }
6 | ],
7 | "info" : {
8 | "author" : "xcode",
9 | "version" : 1
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "platform" : "ios",
6 | "size" : "1024x1024"
7 | }
8 | ],
9 | "info" : {
10 | "author" : "xcode",
11 | "version" : 1
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "author" : "xcode",
4 | "version" : 1
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | UIApplicationSceneManifest
6 |
7 | UIApplicationSupportsMultipleScenes
8 |
9 | UISceneConfigurations
10 |
11 | UIWindowSceneSessionRoleApplication
12 |
13 |
14 | UISceneConfigurationName
15 | Default Configuration
16 | UISceneDelegateClassName
17 | $(PRODUCT_MODULE_NAME).SceneDelegate
18 | UISceneStoryboardFile
19 | Main
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/PhotoView.swift:
--------------------------------------------------------------------------------
1 | //
2 | // PhotoView.swift
3 | // CameraButtonExample
4 | //
5 | // Created by Erik Drobne on 02/12/2022.
6 | //
7 |
8 | import SwiftUI
9 | import CameraButton
10 |
11 | struct PhotoView: View {
12 |
13 | @State var isRecording: Bool = false
14 |
15 | var body: some View {
16 | CameraButtonUI(
17 | size: 72,
18 | borderColor: .red,
19 | fillColor: (.purple, .orange),
20 | progressColor: .green,
21 | progressDuration: 5,
22 | isRecording: self.$isRecording
23 | )
24 | .simultaneousGesture(
25 | TapGesture()
26 | .onEnded { _ in
27 | print("tapped")
28 | }
29 | )
30 | .gesture(
31 | LongPressGesture(minimumDuration: 1)
32 | .onChanged { val in
33 | isRecording = true
34 | }
35 | )
36 | .onChange(of: isRecording, perform: { [isRecording] newValue in
37 | print("isRecording", isRecording, newValue)
38 | })
39 | }
40 | }
41 |
42 | struct PhotoView_Previews: PreviewProvider {
43 | static var previews: some View {
44 | PhotoView()
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/SceneDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // SceneDelegate.swift
3 | // CameraButtonExample
4 | //
5 | // Created by Erik Drobne on 13/10/2022.
6 | //
7 |
8 | import UIKit
9 |
10 | class SceneDelegate: UIResponder, UIWindowSceneDelegate {
11 |
12 | var window: UIWindow?
13 |
14 |
15 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
16 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
17 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
18 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
19 | guard let _ = (scene as? UIWindowScene) else { return }
20 | }
21 |
22 | func sceneDidDisconnect(_ scene: UIScene) {
23 | // Called as the scene is being released by the system.
24 | // This occurs shortly after the scene enters the background, or when its session is discarded.
25 | // Release any resources associated with this scene that can be re-created the next time the scene connects.
26 | // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
27 | }
28 |
29 | func sceneDidBecomeActive(_ scene: UIScene) {
30 | // Called when the scene has moved from an inactive state to an active state.
31 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
32 | }
33 |
34 | func sceneWillResignActive(_ scene: UIScene) {
35 | // Called when the scene will move from an active state to an inactive state.
36 | // This may occur due to temporary interruptions (ex. an incoming phone call).
37 | }
38 |
39 | func sceneWillEnterForeground(_ scene: UIScene) {
40 | // Called as the scene transitions from the background to the foreground.
41 | // Use this method to undo the changes made on entering the background.
42 | }
43 |
44 | func sceneDidEnterBackground(_ scene: UIScene) {
45 | // Called as the scene transitions from the foreground to the background.
46 | // Use this method to save data, release shared resources, and store enough scene-specific state information
47 | // to restore the scene back to its current state.
48 | }
49 |
50 |
51 | }
52 |
53 |
--------------------------------------------------------------------------------
/CameraButtonExample/CameraButtonExample/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // CameraButtonExample
4 | //
5 | // Created by Erik Drobne on 13/10/2022.
6 | //
7 |
8 | import UIKit
9 | import SwiftUI
10 | import CameraButton
11 |
12 | class ViewController: UIViewController {
13 |
14 | override func viewDidLoad() {
15 | super.viewDidLoad()
16 |
17 | setup()
18 | }
19 |
20 | // MARK: - Private methods
21 |
22 | private func setup() {
23 | let cameraButton = CameraButton()
24 | cameraButton.delegate = self
25 |
26 | view.addSubview(cameraButton)
27 |
28 | cameraButton.translatesAutoresizingMaskIntoConstraints = false
29 | NSLayoutConstraint.activate([
30 | cameraButton.widthAnchor.constraint(equalToConstant: 72),
31 | cameraButton.heightAnchor.constraint(equalToConstant: 72),
32 | cameraButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
33 | cameraButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
34 | ])
35 |
36 | cameraButton.borderColor = .red
37 | cameraButton.fillColor = (.purple, .orange)
38 | cameraButton.progressColor = .green
39 |
40 | let button = UIButton()
41 | button.setTitle("CameraButtonUI", for: .normal)
42 | button.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
43 | view.addSubview(button)
44 |
45 | button.translatesAutoresizingMaskIntoConstraints = false
46 | NSLayoutConstraint.activate([
47 | button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
48 | button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -64)
49 | ])
50 | }
51 |
52 | private func cameraButtonUIController() -> UIViewController {
53 | let controller = UIHostingController(rootView: PhotoView())
54 | view.addSubview(controller.view)
55 |
56 | controller.view.translatesAutoresizingMaskIntoConstraints = false
57 | NSLayoutConstraint.activate([
58 | controller.view.topAnchor.constraint(equalTo: view.topAnchor),
59 | controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
60 | controller.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
61 | controller.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
62 | ])
63 |
64 | return controller
65 | }
66 |
67 | @objc func buttonTap() {
68 | self.present(cameraButtonUIController(), animated: true)
69 | }
70 | }
71 |
72 | extension ViewController: CameraButtonDelegate {
73 | func didTap(_ button: CameraButton) {
74 |
75 | }
76 |
77 | func didFinishProgress() {
78 |
79 | }
80 | }
81 |
82 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Erik Drobne
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:5.5
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "CameraButton",
7 | platforms: [
8 | .iOS(.v14)
9 | ],
10 | products: [
11 | .library(name: "CameraButton", targets: ["CameraButton"])
12 | ],
13 | targets: [
14 | .target(name: "CameraButton", dependencies: [], path: "Sources"),
15 | .testTarget(name: "CameraButtonTests", dependencies: ["CameraButton"])
16 | ]
17 | )
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CameraButton
2 |
3 | 
4 | 
5 |
6 | A simple camera button that can be used for photo and video capturing.
7 |
8 | ## Requirements
9 |
10 | **iOS 14.0** or higher
11 |
12 | ## Instalation
13 |
14 | ### Swift Package Manager
15 |
16 | ```Swift
17 | dependencies: [
18 | .package(url: "https://github.com/erikdrobne/CameraButton")
19 | ]
20 | ```
21 |
22 | ## Usage
23 |
24 | ### Import
25 |
26 | ```Swift
27 | import CameraButton
28 | ```
29 |
30 | ### UIKit
31 |
32 | ### Initialize
33 |
34 | ```Swift
35 | let button = CameraButton()
36 | button.delegate = self
37 | view.addSubview(button)
38 | button.translatesAutoresizingMaskIntoConstraints = false
39 |
40 | NSLayoutConstraint.activate([
41 | button.widthAnchor.constraint(equalToConstant: 72),
42 | button.heightAnchor.constraint(equalToConstant: 72),
43 | button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
44 | button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -64)
45 | ])
46 | ```
47 |
48 | ### Customize
49 |
50 | ```Swift
51 | // Set custom colors
52 | button.borderColor = .red
53 | button.fillColor = (.purple, .orange)
54 | button.progressColor = .green
55 |
56 | // Set progress animation duration
57 | button.progressDuration = 5
58 |
59 | // Start progress animation
60 | button.start()
61 |
62 | // Stop progress animation
63 | button.stop()
64 | ```
65 |
66 | ### Delegate
67 |
68 | The `CameraButtonDelegate` requires you to implement the following methods:
69 |
70 | ```Swift
71 | func didTap(_ button: CameraButton)
72 | func didFinishProgress()
73 | ```
74 |
75 | ### SwiftUI
76 |
77 | ```Swift
78 | struct PhotoView: View {
79 |
80 | @State var isRecording: Bool = false
81 | @State var didFinishProgress: Bool = false
82 |
83 | var body: some View {
84 | CameraButtonUI(
85 | size: 72,
86 | borderColor: .red,
87 | fillColor: (.purple, .orange),
88 | progressColor: .green,
89 | progressDuration: 5,
90 | isRecording: self.$isRecording
91 | )
92 | // Handle tap gesture
93 | .simultaneousGesture(
94 | TapGesture()
95 | .onEnded { _ in
96 | print("tap")
97 | }
98 | )
99 | // Start recording on Long-press gesture
100 | .gesture(
101 | LongPressGesture(minimumDuration: 1)
102 | .onChanged { val in
103 | isRecording = true
104 | }
105 | )
106 | // Observe state changes
107 | .onChange(of: isRecording, perform: { [isRecording] newValue in
108 | print("isRecording", isRecording, newValue)
109 | })
110 | }
111 | }
112 | ```
113 |
--------------------------------------------------------------------------------
/Sources/CameraButton/CameraButton.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 | import UIKit
3 |
4 | public protocol CameraButtonDelegate: AnyObject {
5 | /// This method is called on button tap.
6 | func didTap(_ button: CameraButton)
7 | /// This method is called when progress reaches the end of duration.
8 | func didFinishProgress()
9 | }
10 |
11 | public class CameraButton: UIButton, CAAnimationDelegate {
12 |
13 | // MARK: - Private properties
14 |
15 | private let borderLayer = CAShapeLayer()
16 | private let progressLayer = CAShapeLayer()
17 | private let shapeLayer = CAShapeLayer()
18 |
19 | private (set) public var isRecording = false
20 |
21 | /// This struct contains data for layer animations.
22 | private struct Animation {
23 | static let progress = (id: "progress", key: "strokeEnd", index: 0)
24 | static let tap = (id: "tap", key: "transform.scale", index: 1)
25 | }
26 |
27 | // MARK: - Public properties
28 |
29 | public weak var delegate: CameraButtonDelegate?
30 | public var borderColor = UIColor.white
31 | public var fillColor: (default: UIColor, record: UIColor) = (.white, .white)
32 | public var progressColor = UIColor.red
33 | public var progressDuration: TimeInterval = 5
34 |
35 | // MARK: - Initialization
36 |
37 | public override init(frame: CGRect) {
38 | super.init(frame: frame)
39 | setup()
40 | }
41 |
42 | public required init?(coder: NSCoder) {
43 | super.init(coder: coder)
44 | setup()
45 | }
46 |
47 | // MARK: - Lifecycle
48 |
49 | public override func layoutSubviews() {
50 | super.layoutSubviews()
51 |
52 | layer.cornerRadius = min(bounds.width, bounds.height) / 2
53 | setupBorderLayer()
54 | setupProgressLayer()
55 | setupShapeLayer()
56 | }
57 |
58 | // MARK: - Public methods
59 |
60 | /// CameraButton: start progress animation.
61 | public func start() {
62 | guard !isRecording else {
63 | return
64 | }
65 |
66 | isRecording = true
67 | borderLayer.opacity = 0
68 | progressLayer.opacity = 1
69 | shapeLayer.fillColor = fillColor.record.cgColor
70 |
71 | UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: ({
72 | self.backgroundColor = self.fillColor.record.withAlphaComponent(0.6)
73 | }), completion: { _ in
74 | self.animateProgress(duration: self.progressDuration)
75 | })
76 | }
77 |
78 | /// CameraButton: stop progress animation.
79 | public func stop() {
80 | guard isRecording else {
81 | return
82 | }
83 |
84 | isRecording = false
85 | progressLayer.opacity = 0
86 | borderLayer.opacity = 1
87 | shapeLayer.fillColor = fillColor.default.cgColor
88 |
89 | UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: ({
90 | self.backgroundColor = .clear
91 | }), completion: { _ in
92 | self.clearProgressAnimation()
93 | })
94 | }
95 |
96 | // MARK: - Private methods
97 |
98 | private func setup() {
99 | clipsToBounds = false
100 | backgroundColor = .clear
101 | addTarget(self, action: #selector(handleTap), for: .touchUpInside)
102 | }
103 |
104 | private func setupBorderLayer() {
105 | layer.addSublayer(borderLayer)
106 | borderLayer.strokeColor = borderColor.cgColor
107 | borderLayer.lineWidth = frame.width * 0.05
108 | borderLayer.fillColor = nil
109 | borderLayer.position = CGPoint(x: layer.bounds.midX, y: layer.bounds.midY)
110 |
111 | let diameter = frame.width
112 | let rect = CGRect(x: -diameter / 2, y: -diameter / 2, width: diameter, height: diameter)
113 | borderLayer.path = UIBezierPath(ovalIn: rect).cgPath
114 | }
115 |
116 | private func setupShapeLayer() {
117 | layer.addSublayer(shapeLayer)
118 | shapeLayer.fillColor = fillColor.default.cgColor
119 | shapeLayer.position = CGPoint(x: layer.bounds.midX, y: layer.bounds.midY)
120 | shapeLayer.path = UIBezierPath(ovalIn: rect(for: frame.width * 0.87)).cgPath
121 | }
122 |
123 | private func setupProgressLayer() {
124 | layer.addSublayer(progressLayer)
125 | progressLayer.strokeColor = progressColor.cgColor
126 | progressLayer.lineWidth = frame.width * 0.08
127 | progressLayer.opacity = 0
128 | progressLayer.strokeEnd = 0
129 | progressLayer.lineCap = .round
130 | progressLayer.fillColor = nil
131 | progressLayer.position = CGPoint(x: layer.bounds.midX, y: layer.bounds.midY)
132 |
133 | let diameter = frame.width
134 | let path = UIBezierPath(
135 | roundedRect: rect(for: diameter),
136 | byRoundingCorners: .allCorners,
137 | cornerRadii: CGSize(width: diameter, height: diameter)
138 | )
139 |
140 | progressLayer.path = path.cgPath
141 | }
142 |
143 | private func animateProgress(duration t: TimeInterval) {
144 | let animation = CABasicAnimation(keyPath: Animation.progress.key)
145 | animation.delegate = self
146 | animation.duration = t
147 | animation.fromValue = 0
148 | animation.toValue = 1
149 | animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
150 | animation.setValue(Animation.progress.index, forKey: Animation.progress.id)
151 | progressLayer.strokeEnd = 1.0
152 | progressLayer.add(animation, forKey: Animation.progress.key)
153 | }
154 |
155 | private func clearProgressAnimation() {
156 | progressLayer.removeAnimation(forKey: Animation.progress.key)
157 | progressLayer.strokeEnd = 0
158 | progressLayer.opacity = 0
159 | progressLayer.layoutIfNeeded()
160 | }
161 |
162 | private func animateTap(duration t: TimeInterval) {
163 | let animation = CABasicAnimation(keyPath: Animation.tap.key)
164 | animation.duration = t
165 | animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
166 | animation.toValue = [0.9, 0.9]
167 | animation.autoreverses = true
168 | animation.setValue(Animation.tap.index, forKey: Animation.tap.id)
169 | shapeLayer.add(animation, forKey: Animation.tap.key)
170 | }
171 |
172 | @objc private func handleTap(_ sender: CameraButton) {
173 | DispatchQueue.main.async { [weak self] in
174 | UIImpactFeedbackGenerator(style: .light).impactOccurred()
175 | self?.animateTap(duration: 0.15)
176 | self?.delegate?.didTap(sender)
177 | }
178 | }
179 |
180 | // MARK: - Utilities
181 |
182 | private func rect(for diameter: CGFloat) -> CGRect {
183 | return CGRect(x: -diameter / 2, y: -diameter / 2, width: diameter, height: diameter)
184 | }
185 |
186 | // MARK: - CAAnimationDelegate
187 |
188 | public func animationDidStop(_ animation: CAAnimation, finished flag: Bool) {
189 | guard
190 | flag,
191 | animation.value(forKey: Animation.progress.id) as? Int == Animation.progress.index
192 | else {
193 | return
194 | }
195 |
196 | DispatchQueue.main.async { [weak self] in
197 | self?.stop()
198 | self?.delegate?.didFinishProgress()
199 | }
200 | }
201 | }
202 |
--------------------------------------------------------------------------------
/Sources/CameraButtonUI/CameraButtonUI.swift:
--------------------------------------------------------------------------------
1 | //
2 | // CameraButtonUI.swift
3 | //
4 | //
5 | // Created by Erik Drobne on 14/10/2022.
6 | //
7 |
8 | import SwiftUI
9 |
10 | public struct CameraButtonUI: View {
11 |
12 | private typealias ProgressDuration = (record: TimeInterval, cleanup: TimeInterval)
13 |
14 | // MARK: - Private properties
15 |
16 | @Binding private var isRecording: Bool
17 | @State private var scalingFactor: CGFloat = 1
18 | @State private var percentage = CGFloat.zero
19 |
20 | private let borderColor: Color
21 | private let fillColor: (default: Color, record: Color)
22 | private let progressColor: Color
23 |
24 | private let duration: ProgressDuration
25 | private let size: CGFloat
26 | private let feedback = UIImpactFeedbackGenerator(style: .light)
27 |
28 | private var center: CGPoint {
29 | return CGPoint(x: size * 0.5, y: size * 0.5)
30 | }
31 |
32 | public init(
33 | size: CGFloat = 72,
34 | borderColor: Color = .white,
35 | fillColor: (`default`: Color, record: Color) = (.white, .white),
36 | progressColor: Color = .red,
37 | progressDuration: TimeInterval,
38 | isRecording: Binding
39 | ) {
40 | self.size = size
41 | self.borderColor = borderColor
42 | self.fillColor = fillColor
43 | self.progressColor = progressColor
44 | self.duration = (progressDuration, 0.2)
45 | self._isRecording = isRecording
46 | }
47 |
48 | public var body: some View {
49 | ZStack {
50 | if isRecording {
51 | Circle()
52 | .fill(borderColor)
53 | .frame(width: size, height: size)
54 | } else {
55 | Circle()
56 | .strokeBorder(borderColor, lineWidth: size * 0.05)
57 | .frame(width: size, height: size)
58 | }
59 | Circle()
60 | .fill(fillColor.default)
61 | .padding(size * 0.08)
62 | .frame(width: size, height: size)
63 | .modifier(ReversingScale(to: scalingFactor) {
64 | DispatchQueue.main.async {
65 | self.scalingFactor = 1
66 | }
67 | })
68 | .animation(.easeInOut(duration: 0.15), value: scalingFactor)
69 |
70 | Circle()
71 | .trim(from: 0, to: percentage)
72 | .stroke(
73 | progressColor,
74 | style: StrokeStyle(lineWidth: size * 0.08, lineCap: .round)
75 | )
76 | .rotationEffect(-.radians(.pi)/2)
77 | .padding(size * 0.01)
78 | .frame(width: size, height: size)
79 | }
80 | .frame(width: size, height: size)
81 | .onTapGesture {
82 | didTap()
83 | }
84 | .onChange(of: isRecording, perform: { isRecording in
85 | if isRecording {
86 | start()
87 | } else {
88 | stop()
89 | }
90 | })
91 | }
92 |
93 | // MARK: - Private methods
94 |
95 | private func didTap() {
96 | self.scalingFactor = 0.9
97 |
98 | guard isRecording else {
99 | return
100 | }
101 |
102 | isRecording = false
103 | }
104 |
105 | private func clear() {
106 | withAnimation(.linear(duration: duration.cleanup)) {
107 | self.percentage = 0
108 | }
109 | }
110 |
111 | private func start() {
112 | feedback.impactOccurred()
113 |
114 | withAnimation(.linear(duration: duration.record)) {
115 | self.percentage = 1.0
116 | }
117 | }
118 |
119 | private func stop() {
120 | feedback.impactOccurred()
121 | clear()
122 | }
123 | }
124 |
125 | struct SwiftUIView_Previews: PreviewProvider {
126 | static var previews: some View {
127 | CameraButtonUI(progressDuration: 5, isRecording: .constant(false))
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/Sources/CameraButtonUI/ReversingScale.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ReversingScale.swift
3 | //
4 | //
5 | // Created by Erik Drobne on 23/11/2022.
6 | //
7 |
8 | import Foundation
9 | import SwiftUI
10 |
11 | struct ReversingScale: AnimatableModifier {
12 | var value: CGFloat
13 |
14 | private let target: CGFloat
15 | private let onEnded: () -> ()
16 |
17 | init(to value: CGFloat, onEnded: @escaping () -> () = {}) {
18 | self.target = value
19 | self.value = value
20 | self.onEnded = onEnded
21 | }
22 |
23 | var animatableData: CGFloat {
24 | get { value }
25 | set { value = newValue
26 | let callback = onEnded
27 | if newValue == target {
28 | DispatchQueue.main.async(execute: callback)
29 | }
30 | }
31 | }
32 |
33 | func body(content: Content) -> some View {
34 | content.scaleEffect(value)
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Tests/CameraButtonTests/CameraButtonTests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 | @testable import CameraButton
3 |
4 | final class CameraButtonTests: XCTestCase {
5 | func testExample() throws {
6 | // This is an example of a functional test case.
7 | // Use XCTAssert and related functions to verify your tests produce the correct
8 | // results.
9 | }
10 | }
11 |
--------------------------------------------------------------------------------