Skip to content

Instantly share code, notes, and snippets.

View AnselmeKotchap's full-sized avatar

Anselme AnselmeKotchap

View GitHub Profile
@AnselmeKotchap
AnselmeKotchap / gist:f5111282b431ecb94102676a20c9c2e1
Created August 19, 2018 22:55 — forked from bwhiteley/gist:049e4bede49e71a6d2e2
Initialize Swift subclass of UIView, designed in .xib
// Create CustomView.xib, set File's Owner to CustomView.
// Link the top level view in the XIB to the contentView outlet.
class CustomView : UIView {
@IBOutlet private var contentView:UIView?
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
@AnselmeKotchap
AnselmeKotchap / godaddydomain.md
Created November 22, 2017 15:38 — forked from yancyn/godaddydomain.md
Setup Godaddy Domain for GitHub Page

Setup Godaddy Domain for GitHub Page

  1. Create a new file CNAME and put the domain.com in the file.
  2. Login GoDaddy > Manage domain > DNS Zone File.
  3. Under A (Host) change @ point to 192.30.252.153.
  4. Under CName (Alias) change www point to website.github.io.

see also GitHub Pages + GoDaddy

@AnselmeKotchap
AnselmeKotchap / beautiful_idiomatic_python.md
Created October 5, 2016 10:58 — forked from JeffPaine/beautiful_idiomatic_python.md
Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]:
@AnselmeKotchap
AnselmeKotchap / basicanimation.swift
Created October 3, 2016 00:30
some basic animation: shake, rotate, zoom in out
//shake animation: http://stackoverflow.com/questions/27987048/shake-animation-for-uitextfield-uiview-in-swift
public extension UIView {
func shake(count : Float? = nil,for duration : TimeInterval? = nil,withTanslation translation : Float? = nil) {
let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.repeatCount = count ?? 2
animation.duration = (duration ?? 0.5)/TimeInterval(animation.repeatCount)
animation.autoreverses = true
// so it won't compile on a lower version.
extension UIAlertController {
func show() {
present(animated: true, completion: nil)
}
func present(animated animated: Bool, completion: (() -> Void)?) {
if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController {
presentFromController(rootVC, animated: animated, completion: completion)
extension UIViewController {
public var isVisible: Bool {
if isViewLoaded() {
return view.window != nil
}
return false
}
public var isTopViewController: Bool {
if self.navigationController != nil {
extension UIDevice {
var iPhone: Bool {
return UIDevice().userInterfaceIdiom == .Phone
}
enum ScreenType: String {
case iPhone4
case iPhone5
case iPhone6
case iPhone6Plus
case Unknown
import Foundation
class Timer {
var timer = NSTimer()
var handler: (Int) -> ()
let duration: Int
var elapsedTime: Int = 0
init(duration: Int, handler: (Int) -> ()) {
//While I'm not sure the details of your implementation, I'd probably recommend storing the start time as an NSDate(),
//then instead of incrementing/decrementing from an Integer/Float/Double, you can just calculate the time since the initial
//date, then remove that amount of time from your totalTime. That way when the app reopens, the time calculation is always
//correct. The calculation for determining how long ago an NSDate() is so light that it'll be nearly instantaneous.
import UIKit
class Timer {
private var startTime: NSDate? // Recorded when the user starts the timer.
private var totalTime: Double = 0.0 // If you're counting down, this is the total time designated by the user (e.g. 20 mins)
//2 functions to pause and resume animation, I take from here and convert to Swift.
func pauseLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
func resumeLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.timeOffset