Skip to content

Instantly share code, notes, and snippets.

View wddwycc's full-sized avatar
🎯
Focusing

duan wddwycc

🎯
Focusing
View GitHub Profile
@wddwycc
wddwycc / String+match.swift
Created August 31, 2016 03:53
简单的正则匹配方法
extension String {
func matches(pattern: String, options: NSRegularExpressionOptions = []) -> Bool {
do {
let regex = try NSRegularExpression(pattern: pattern, options: options)
return (regex.firstMatchInString(self, options: [], range: NSRange(location: 0, length: self.characters.count)) != nil)
} catch {
return false
}
}
}
@wddwycc
wddwycc / anime.swift
Created June 13, 2016 01:46
UIView basic animations
extension UIView {
func shake() {
let anim = CAKeyframeAnimation( keyPath:"transform" )
anim.values = [
NSValue( CATransform3D: CATransform3DMakeTranslation(-5, 0, 0 ) ),
NSValue( CATransform3D: CATransform3DMakeTranslation( 5, 0, 0 ) )
]
anim.autoreverses = true
anim.repeatCount = 2
anim.duration = 7/100
@wddwycc
wddwycc / popup.swift
Created May 30, 2016 15:34
iOS里用创建popUp的时候让它始终呈现在最顶层的办法.
if(!self.superview){
NSEnumerator *frontToBackWindows = [[[UIApplication sharedApplication] windows] reverseObjectEnumerator];
for (UIWindow *window in frontToBackWindows) {
if (window.windowLevel == UIWindowLevelNormal) {
[window addSubview:self];
break;
}
}
@wddwycc
wddwycc / CanvasGame.js
Created May 29, 2016 16:51
简单的canvas游戏
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);
@wddwycc
wddwycc / MenuPop.swift
Created April 28, 2016 15:42
从某个点pop出来menu(OS X)
let delegate = NSApplication.sharedApplication().delegate as! AppDelegate
let menu = NSMenu()
menu.addItem(NSMenuItem(title: "Apps", action: #selector(delegate.openAppsLink), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "FAQ", action: #selector(delegate.openFAQLink), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "About", action: #selector(delegate.openAboutLink), keyEquivalent: ""))
menu.addItem(NSMenuItem.separatorItem())
menu.addItem(NSMenuItem(title: "Check for Updates", action: #selector(delegate.checkForUpdates), keyEquivalent: "u"))
menu.addItem(NSMenuItem(title: "Settings", action: #selector(delegate.showSettings), keyEquivalent: "s"))
@wddwycc
wddwycc / mocking.swift
Created March 24, 2016 02:48
func里定义class, 实现可mocking的封装.~ Unit Test
func testFetchRequestWithMockedManagedObjectContext() {
class MockNSManagedObjectContext: NSManagedObjectContext {
override func executeFetchRequest(request: NSFetchRequest!, error: AutoreleasingUnsafePointer<NSError?>) -> [AnyObject]! {
return [["name": "Johnny Appleseed", "email": "[email protected]"]]
}
}
let mockContext = MockNSManagedObjectContext()
let fetchRequest = NSFetchRequest(entityName: "User")
fetchRequest.predicate = NSPredicate(format: "email ENDSWITH[cd] %@", "@apple.com")
@wddwycc
wddwycc / async.swift
Created March 24, 2016 02:41
完整的网络异步测试Unit Test
func testAsynchronousURLConnection() {
let URL = NSURL(string: "http://nshipster.com/")!
let expectation = expectationWithDescription("GET \(URL)")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(URL) { data, response, error in
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")
if let HTTPResponse = response as? NSHTTPURLResponse,
@wddwycc
wddwycc / SSImageVC.swift
Created March 15, 2016 15:20
one page image viewer
//
// SSImageVC.swift
// SSImageVC
//
// Created by Carrl on 16/3/7.
// Copyright © 2016年 monk-studio. All rights reserved.
//
import UIKit
@wddwycc
wddwycc / grayScale.HTML
Created March 12, 2016 14:07
从一个彩色图到灰阶图的例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<img src="xx.jpg" alt="My Avatar" id="avatar" title="aaa"/>
@wddwycc
wddwycc / drawText.swift
Created February 28, 2016 11:37
CoreGraphics画字
UIGraphicsBeginImageContextWithOptions(CGSize(), false, UIScreen.mainScreen().scale)
//// Text Drawing
let textRect = CGRectMake(0, 0, 0, 14)
let textTextContent = NSString(string: skillLocal)
let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
textStyle.alignment = .Center
let rectanglePath = UIBezierPath(roundedRect: CGRectMake(), cornerRadius:)
fillColor.setFill()
rectanglePath.fill()