Skip to content

Instantly share code, notes, and snippets.

@zxx
zxx / turn screen on, keep screen on , disable keyguard
Last active August 29, 2015 14:06
Turn screen on / Keep screen on / Disable keyguard
// only keep screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// disable KeyGuard and turn screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
// disable KeyGuard then turn screen on and keep screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
@zxx
zxx / gist:d5ba9caf800bd91d6751
Created June 3, 2015 15:02
ios force screen orientation
// 强制横屏
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
@zxx
zxx / Version.m
Last active August 29, 2015 14:27 — forked from alex-cellcity/Version.m
Runtime iOS Version Checking
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(<#Original Selector#>);
SEL swizzledSelector = @selector(<#Swizzled Selector#>);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
@zxx
zxx / http request retry
Last active September 5, 2016 03:42
iOS HTTP 请求失败后重试 n 次
// https://github.com/AFNetworking/AFNetworking/issues/393
- (void)downloadFileRetryingNumberOfTimes:(NSUInteger)ntimes
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
if (ntimes <= 0) {
if (failure) {
NSError *error = ...;
failure(error);
}
@zxx
zxx / NSShadow Example
Created May 4, 2016 09:33 — forked from peerasak-u/NSShadow Example
NSShadow Example
NSShadow *textShadow = [[NSShadow alloc] init];
textShadow.shadowColor = [UIColor darkGrayColor];
textShadow.shadowBlurRadius = 1.2;
textShadow.shadowOffset = CGSizeMake(1,1);
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSShadowAttributeName: textShadow
};
@zxx
zxx / qsort.swift
Created November 29, 2016 00:59 — forked from onevcat/qsort.swift
Quick sort of Swift
//: Playground - noun: a place where people can play
import UIKit
var randomNumbers = [42, 12, 88, 62, 63, 56, 1, 77, 88, 97, 97, 20, 45, 91, 62, 2, 15, 31, 59, 5]
func partition(inout v: [Int], left: Int, right: Int) -> Int {
var i = left
for j in (left + 1) ... right {
if v[j] < v[left] {
@zxx
zxx / xcodeproj_build.rb
Created November 29, 2016 01:00 — forked from onevcat/xcodeproj_build.rb
Example of using xcodeproj
require_relative "../config.rb"
gem 'xcodeproj', '>=0.19.4'
require 'xcodeproj'
PROJECT = 'Unity-iPhone'
TARGET = 'Unity-iPhone'
LIBRARY = 'Libraries'
### Export
def exportUnityProject
@zxx
zxx / Install_tmux
Last active January 14, 2017 04:50 — forked from simme/Install_tmux
Install and configure tmux on Mac OS X
# First install tmux
brew install tmux
# For mouse support (for switching panes and windows)
# Only needed if you are using Terminal.app (iTerm has mouse support)
Install http://www.culater.net/software/SIMBL/SIMBL.php
Then install https://bitheap.org/mouseterm/
# More on mouse support http://floriancrouzat.net/2010/07/run-tmux-with-mouse-support-in-mac-os-x-terminal-app/
ACTION = build
AD_HOC_CODE_SIGNING_ALLOWED = NO
ALTERNATE_GROUP = staff
ALTERNATE_MODE = u+w,go-w,a+rX
ALTERNATE_OWNER = grantdavis
ALWAYS_SEARCH_USER_PATHS = NO
ALWAYS_USE_SEPARATE_HEADERMAPS = YES
APPLE_INTERNAL_DEVELOPER_DIR = /AppleInternal/Developer
APPLE_INTERNAL_DIR = /AppleInternal
APPLE_INTERNAL_DOCUMENTATION_DIR = /AppleInternal/Documentation