Skip to content

Instantly share code, notes, and snippets.

View marlonjames71's full-sized avatar
💻
Learning Swift & SwiftUI

Marlon Raskin marlonjames71

💻
Learning Swift & SwiftUI
View GitHub Profile
// For one dimensional array
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = mediaItems[indexPath.item]
let pagedMediaViewController = PagedMediaController(mediaItem: item, currentIndex: indexPath.item)
pagedMediaViewController.modalPresentationStyle = .overFullScreen
present(pagedMediaViewController, animated: true)
}
// In case you're dealing with a two dimensional array like I was dealing with since the collection view's items
// had sections
class MediaViewController: UIViewController {
let mediaItem: MediaItem
init(mediaItem: MediaItem) {
self.mediaItem = mediaItem
}
required init(coder: NSCoder) {
fatalError("Use init(mediaItem: MediaItem)")
}
class PagedMediaViewController: UIPageViewController {
// MARK: - Properties
private var mediaItems: [MediaItem]
private var currentIndex: Int
// MARK: - Init
init(mediaItems: [MediaItem], currentIndex: Int) {
self.mediaItems = mediaItems
self.currentIndex = currentIndex
struct MediaItem {
let id: UUID
let mediaType: MediaType
let title: String?
let mediaUrl: URL?
}
enum MediaType { case video, photo }
// For one dimensional array
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = mediaItems[indexPath.item]
let pagedMediaViewController = PagedMediaController(mediaItem: item, currentIndex: indexPath.item)
pagedMediaViewController.modalPresentationStyle = .overFullScreen
present(pagedMediaViewController, animated: true)
}
// In case you're dealing with a two dimensional array like I was dealing with since the collection view
// had sections
@marlonjames71
marlonjames71 / RoundCornersWithMaskInUIKit.swift
Created April 26, 2021 17:20
Can specify which corners of a `UIView` are rounded. Extended `CACornerMask` OptionSet to be a bit more readable and friendlier to use.
extension UIView {
/// Applies rounded corners with masking to specific corners and with corner curve option.
func roundCorners(cornerRadius: CGFloat, roundedStyle: CALayerCornerCurve, corners: CACornerMask) {
layer.cornerRadius = cornerRadius
layer.cornerCurve = roundedStyle
layer.maskedCorners = corners
}
}
@marlonjames71
marlonjames71 / UIKitPreviews.swift
Created April 23, 2021 23:53
Create previews to use in UIKit
struct MyViewControllerClass_Previews: PreviewProvider {
static var previews: some View {
UIViewControllerPreview {
return MyViewControllerClass()
}
.previewDevice("iPhone SE (2nd generation)")
}
}
@marlonjames71
marlonjames71 / StringQueryParserUsingNSLinguisticsTagger.swift
Last active April 2, 2021 06:53
Orginally this was used for a project I worked on where the user was creating a sort of journal entry that had a title. You'd pass the title into this parser and when the user wanted to add a cover photo to the entry using Unsplash, the parser would parse the entry title so when Unsplash appears, it already showed relevant results to the entry.
// This was originally done in Playgrounds
// You can plop this in Playgrounds and it should work.
import UIKit
let title = "The best camping trip of the year with the best friends"
func checkForValidQueryString(_ string: String) -> Bool {
guard
!string.isEmpty,
@marlonjames71
marlonjames71 / ColorExt.swift
Last active March 29, 2021 00:19
DropDownMenu Code Snippets
extension Color {
static let bg = Color("mainBG")
static let secondaryBG = Color("secondaryBG")
static let greyText = Color("greyText")
}
@marlonjames71
marlonjames71 / NSobjectProtocolConformance.swift
Last active July 17, 2020 15:55
NSObject Protocol Conformance
import Foundation
protocol NextObject: AnyObject, Hashable {
func isEqual(_ object: Any?) -> Bool
var hash: Int { get }
var superclass: AnyClass? { get }
func `self`() -> Self