Skip to content

Instantly share code, notes, and snippets.

@leilee
leilee / UIView+TimingFunction.m
Last active November 29, 2017 03:22
#UIKit #animation
+ (void)animateWithDuration:(NSTimeInterval)duration
timingFunction:(CAMediaTimingFunction*)timingFunction
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
{
[CATransaction begin];
[CATransaction setAnimationDuration:duration];
[CATransaction setAnimationTimingFunction:timingFunction];
[UIView animateWithDuration:duration animations:animations completion:completion];
@leilee
leilee / ClosureSelector.swift
Last active November 24, 2017 04:31 — forked from avighnash/ClosureSelector.swift
#UIKit #UIControl #gesture
class ClosureSleeve {
let closure: () -> ()
init(attachTo: AnyObject, closure: @escaping () -> ()) {
self.closure = closure
objc_setAssociatedObject(attachTo, "[\(arc4random())]", self, .OBJC_ASSOCIATION_RETAIN)
}
@objc func invoke() {
closure()
@leilee
leilee / Int.swift
Last active November 24, 2017 04:32
#Foundation
extension Int {
func times(_ closure: (Int) throws -> Void) rethrows {
try (0...self).forEach {
try closure($0)
}
}
@discardableResult
func times<T>(_ closure: (Int) throws -> T) rethrows -> [T] {
var result: [T] = []
@leilee
leilee / README.md
Last active November 1, 2017 11:26
Wireshark installed via homebrew cask toasts 'Permission denied'

Just launch Wireshark from terminal:

sudo /Applications/Wireshark.app/Contents/MacOS/Wireshark

@leilee
leilee / UIColor+Random.swift
Created October 30, 2017 10:36
Generate random UIColor
extension UIColor {
class var random: UIColor {
get {
let hue : CGFloat = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
let saturation : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from white
let brightness : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from black
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
}
@leilee
leilee / PlayOperationQueue.mm
Last active October 20, 2017 09:06
NSOperationQueue executes NSOperation serially
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
auto opMaker = ^NSOperation*(int idx) {
return [NSBlockOperation blockOperationWithBlock:^{
sleep(1);
NSLog(@"opration %zd finished", idx);
}];
};
@leilee
leilee / Install iOS Simulators in Xcode Manually.md
Last active May 15, 2024 01:03
Install iOS Simulators in Xcode Manually
  • Open Xcode -> Preferences -> Components.
  • Open the Console App, clear the console.
  • Go back to the Xcode preferences. Start the simulator download, then cancel it.
  • Now in the Console, you will see something about the cancellation with the download URL.
  • Copy the URL from the Console, download it.
  • Copy this file to ~/Library/Caches/com.apple.dt.Xcode/Downloads.
    • If Downloads did not exist, create a new Downloads directory.
    • If Downloads exists, remove all *.dvtdownloadableindex files under it.
  • Open Xcode -> Preferences -> Components, start the simulator download again, it should find the file you downloaded and install it.
@leilee
leilee / NSCacheBenchmark.m
Last active October 20, 2017 09:06
NSCache benchmarking
+ (void)memoryCacheBenchmark
{
NSMutableDictionary* dict = [NSMutableDictionary new];
NSCache* cache = [NSCache new];
int count = 10000, repeat = 100;
NSMutableArray* keys = [NSMutableArray new];
NSMutableArray* values = [NSMutableArray new];
for (int i = 0; i < count; i++)
{
@leilee
leilee / ShowCamera.m
Created September 28, 2017 07:03
Present UIImagePickerController for capture video
- (void)showCamera
{
auto showCamera = ^{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// TODO: alert
return;
}
auto availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
@leilee
leilee / CGGeometry+Overloading.h
Created September 25, 2017 10:04
Operator overloading for CGGeometry
#pragma once
#import <CoreGraphics/CoreGraphics.h>
#pragma mark - CGSize
CG_INLINE
CGSize operator*(const CGSize& lhs, CGFloat f)
{
return (CGSize){lhs.width * f, lhs.height * f};