Skip to content

Instantly share code, notes, and snippets.

View cyrilchandelier's full-sized avatar

Cyril Chandelier cyrilchandelier

View GitHub Profile
import UIKit
@IBDesignable final class MediaProgressBar: UIControl {
// MARK: - Configuration
@IBInspectable var isThumbVisible: Bool = true {
didSet {
thumbView.alpha = isThumbVisible ? 1.0 : 0.0
}
}
@cyrilchandelier
cyrilchandelier / MinimumTouchAreaButton.swift
Created September 23, 2019 07:30
A Swift UIButton extension to ensure the hit-zone is at least 44x44
import UIKit
class MinimumTouchAreaButton: UIButton {
override func hitTest(_ point: CGPoint, with _: UIEvent?) -> UIView? {
guard !isHidden, isUserInteractionEnabled, alpha > 0 else {
return nil
}
let expandedBounds = bounds.insetBy(dx: min(bounds.width - 44.0, 0), dy: min(bounds.height - 44.0, 0))
return expandedBounds.contains(point) ? self : nil
}
@cyrilchandelier
cyrilchandelier / OptionalAssignment.swift
Last active September 19, 2019 06:09
A Swift operator to only assign a value when the right operand is not nil
import Foundation
precedencegroup OptionalAssignment {
associativity: right
}
infix operator ?=: OptionalAssignment
public func ?= <T>(variable: inout T, value: T?) {
if let unwrapped = unwrap(any: value) ?? nil {
@cyrilchandelier
cyrilchandelier / UICollectionView+Registration.swift
Created September 2, 2019 01:32
UICollectionView extension to easily register/dequeue cells
import UIKit
protocol UICollectionViewRegisterable {
static var cellIdentifier: String { get }
static var cellNib: UINib { get }
}
extension UICollectionView {
func register(type: UICollectionViewRegisterable.Type) {
register(type.cellNib, forCellWithReuseIdentifier: type.cellIdentifier)
@cyrilchandelier
cyrilchandelier / UIScrollView+Bounce.swift
Created August 21, 2019 01:42
An extension to detect if scrollview is bouncing
extension UIScrollView {
var isBouncing: Bool {
return isBouncingTop || isBouncingBottom
}
var isBouncingTop: Bool {
return contentOffset.y < topInsetForBouncing - contentInset.top
}
var isBouncingBottom: Bool {
@cyrilchandelier
cyrilchandelier / AppDelegate.swift
Created April 22, 2018 03:44
Detecting organic vs. local notification vs. remote notification app launches
func applicationDidBecomeActive(_ application: UIApplication) {
let notificationController = NotificationController.shared
// Track app launch if the notificationController didn't already do it
if !notificationController.handledLaunchAnalytics {
Analytics.trackAppLaunched(source: .Organic)
}
}
@cyrilchandelier
cyrilchandelier / BaseAppDelegate.swift
Created April 22, 2018 02:32
AppDelegate superclass to handle first launch detection and upgrade detection
//
// BaseAppDelegate.swift
//
// Created by Cyril Chandelier on 21/04/2018.
// Copyright © 2018 Cyril Chandelier. All rights reserved.
//
import UIKit
class BaseAppDelegate: UIResponder, UIApplicationDelegate {
@cyrilchandelier
cyrilchandelier / UILabel+Stylesheet.h
Created October 22, 2015 09:08
Stylesheet like IBInspectable labels
//
// UILabel+Stylesheet.h
// Stylesheet
//
// Created by Cyril Chandelier on 21/10/15.
// Copyright © 2015 CyrilChandelier. All rights reserved.
//
#import <UIKit/UIKit.h>
@cyrilchandelier
cyrilchandelier / NSString+CJK.h
Last active October 30, 2015 10:59
Asian characters detection
#import <Foundation/Foundation.h>
@interface NSString (CJK)
/**
Detect if the string contains Chinese, Japanese or Korean characters
@return YES if the string contains CJK characters, NO otherwise
*/