Skip to content

A collection of Swift snippets implemented in actual projects.

License

Notifications You must be signed in to change notification settings

elliott-io/SwiftSnippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Snippets Icon

Swift Snippets

A collection of Swift snippets implemented in actual projects.

When you run the app you will see a list of snippets, tapping one will navigate to another ViewController with an example implementation of the snippet.

Storyboard Snippets

This project contains snippet implementations in an Xcode project that uses storyboards for UI. Built in Swift 5 for iOS 13.2.

How to Use

Launching the project will display the SnippetsViewController.

On viewDidLoad, SnippetManager reads the local snippet.json file and converts the data into an array of Snippets.

This Snippet array is displayed as custom SnippetCell's. Tapping a SnippetCell will navigate to an example of any given snippet.

Source files related to a snippet are located in Snippets within an appropriate subdirectory, such as SavePhotoCustomAlbum. UI independent source files are displayed as a list the Snippet.

Extensions.swift and Styles.swift are referenced throughout this project and are located under Helpers. More information in their own sections below.

Save Photo to Custom Album

Selecting this snippet, you will see a GalleryViewController titled Asset Gallery displaying some images embedded in Assets.xcassets as custom PhotoCell's.

Each PhotoCell's image fades in as they are loaded using CoreAnimation.

Selecting a photo in this Asset Gallery will display a save button and highlight the PhotoCell.

Tapping the save button will save the image to a custom album declared as albumName in CustomPhotoAlbum.swift.

Once saved, you will see an alert with a small thumbnail of the image in the alert header.

ProTip: If you copy these source files into another project, make sure you add entries to your info.plist for NSPhotoLibraryUsageDescription and NSPhotoLibraryAddUsageDescription.

Popover Messages

Selecting this snippet, you will see a PopoverPlacementViewController titled Popover Placement displaying a segmented control and a few buttons.

A PopoverViewController will be displayed when interacting with these items.

ProTip: You'll notice in the extension function for showPopover that AlwaysPresentAsPopover is used to make the PopoverViewController present as an actual popover, rather than a default view controller.

Taptic (Haptic Feedback)

Selecting this snippet, you will see a TapticViewController titled Taptic Feedback displaying a picker and a button to feel the selected taptic feedback.

TapticGenerator can be run from anywhere in a project to give haptic feedback.

This is used in this project when swiping through cells. It is called from the extension function targetContentOffset

Extensions

Several extensions were written in Extensions.swift to help facilitate common functions that are used across snippets.

Short swipes scroll cell-by-cell in a collection view. Long swipes will scroll multiple items.

Returns the top UIViewController in the app heirarchy. Useful for presenting alerts from any source file.

Displays an alert on any view controller with parameters for title, message, action name (which will appear on the alert button), and an optional image in corner of the header.

Displays an automatically-sized popover on any view controller with parameters for message, source view, source rect (within that source view), passthrough views (which can still be interacted without dismissing the popover), and arrow direction. Dismisses existing presented view controller if one exists (iOS will not allow you to present more than one view controller at a time).

Returns the visual size of text with parameters for default width, font, and font weight. Used to determine popover preferred size based on how long the message text is.

Styles

Light and Dark themes are supported using storyboard theming and a global Styles.swift for consistency.

Swift UI Snippets

Coming soon...

Licensing

All code is free to be used personally or commercially under the MIT License.

Support This Project

If you've found this useful, consider buying me a coffee.

ko-fi

About

A collection of Swift snippets implemented in actual projects.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages