Skip to content

Instantly share code, notes, and snippets.

View victorchee's full-sized avatar
🎯
Focusing

Victor Chee victorchee

🎯
Focusing
View GitHub Profile
@victorchee
victorchee / BinaryUnitTransform.m
Last active August 29, 2015 14:21
Binary unit transform
typedef NS_ENUM(NSUInteger, BinaryUnit) {
b,
B,
KB,
MB,
GB,
TB,
PB,
ZB,
YB
@victorchee
victorchee / BinaryUnitTransform.swift
Last active August 29, 2015 14:21
binary unit transform
public enum BinaryUnit: Int {
case b = 0, B, KB, MB, GB, TB, PB, ZB, YB
}
public func binaryUnitTransform(value: Double, #unit: BinaryUnit) -> (value: Double, unitString: String) {
var raw = unit.rawValue
if value < 1.0 && value > 0 && unit != .b {
return binaryUnitTransform(value * 1024.0, unit: BinaryUnit(rawValue: --raw)!)
} else if value > 1024.0 && unit != .YB {
return binaryUnitTransform(value / 1024.0, unit: BinaryUnit(rawValue: ++raw)!)
@victorchee
victorchee / IpaBuilder
Created June 26, 2015 01:28
Build ipa
#!/bin/bash
#--------------------------------------------
# 功能:编译xcode项目并打ipa包
# 使用说明:
# 编译project
# ipa-build <project directory> [-c <project configuration>] [-o <ipa output directory>] [-t <target name>] [-n] [-p <platform identifier>]
# 编译workspace
# ipa-build <workspace directory> -w -s <schemeName> [-c <project configuration>] [-n]
#
@victorchee
victorchee / EnablePasteAction
Created June 29, 2015 08:57
判断是否允许'paste', 'select', 'selectAll'编辑
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
if (action == @selector(paste:))
return NO;
if (action == @selector(select:))
return NO;
if (action == @selector(selectAll:))
return NO;
@victorchee
victorchee / TextFieldDidChange
Created July 1, 2015 02:16
TextFieldDidChange
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
// do something
return YES;
}
@victorchee
victorchee / PrintDirectoryContent.swift
Created July 21, 2015 11:26
Print Directory Content
func printDirectoryContent(path: String) {
let contents = NSFileManager.defaultManager().contentsOfDirectoryAtPath(path, error: nil)
if let files = contents {
for file in files as! [String] {
let filePath = path.stringByAppendingPathComponent(file)
let attributes = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: nil)!
if attributes[NSFileType] as! String == NSFileTypeDirectory {
printDirectoryContent(filePath)
} else {
if NSFileManager.defaultManager().isUbiquitousItemAtURL(NSURL(fileURLWithPath: filePath)!) {
@victorchee
victorchee / SawtoothView.m
Created August 12, 2015 01:08
draw a sawtooth view
- (void)drawRect:(CGRect)rect
{
const CGFloat frequency = 25.0f;
const CGFloat amplitude = 5.0f;
const CGFloat baseLineHeight = 50.0f; // if base line height equal to amplitude, the sawtooth is on the edge of the view
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0f); // set line width
CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor); // set stroke color
@victorchee
victorchee / SocialShare.m
Created August 12, 2015 01:21
Social share in iOS
#import "SocialShare.h"
#import <Social/Social.h>
@interface SocialShare()
{
SLComposeViewController *composeViewController;
}
@end
@victorchee
victorchee / KeyboardNotification.m
Created December 10, 2015 06:25
Handle keyboard notification
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
@victorchee
victorchee / PagedScroll.m
Created December 11, 2015 06:51
Scroll with page control.
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger pageIndex = lroundf(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame));
pageControl.currentPage = pageIndex;
}