Latest 1.0.0-beta.1
Homepage https://github.com/jameslintaylor/mime
License MIT
Platforms ios 9.0
Dependencies AssociatedObjects
Frameworks UIKit

from target-action to closure! (and beyond 🚀)

mime provides an alternate way of interacting with the objective-c style target-action pattern using swift closures

gestures…

target-action

let tap = UITapGestureRecognizer(target: self, action: #selector(...handleTap(_:))
view.addGestureRecognizer(tap)

dynamic func handleTap(tap: UITapGestureRecognizer) {
    print("tap at (tap.locationInView(view))!")
}

mime

let tap = UITapGestureRecognizer()
tap.mime_on { 
    print("tap at ($0.locationInView($0.view))!")
}
view.addGestureRecognizer(tap)

💥bonus!💥 mime can also do gesture state filtering

let pan = UIPanGestureRecognizer()
pan.mime_on([.began, .changed]) { _ in
    print("meh... I'm not really interested in being called when the pan ends")
}
view.addGestureRecognizer(pan)

controls…

let button = UIButton()
button.mime_on(.allTouchEvents) { 
    print("getting all touchy with ($0.currentTitle)...")
}
view.addSubview(button)

removing a mime closure…

mime_off removes all closures set up with mime_on

tap.mime_off()
button.mime_off()

a note on retain cycles…

// when calling `T.mime_on`, note that `T` will hold a reference to the closure
tap.mime_on {
    print("I'm being retained!")
}

// if you plan on referencing an object that holds reference to `T` from inside the closure, make sure to do so weakly
tap.mime_on { [weak view] in
    print("I'm being retained but it's cool cause I don't really care about (view)")
}
view.addGestureRecognizer(tap)

installation

pod 'mime' and you’re set!

Latest podspec

{
    "name": "mime",
    "version": "1.0.0-beta.1",
    "summary": "from target-action to closure! (and beyond ud83dude80)",
    "description": "mime provides an alternate way of interacting with the objective-c style target-actionnpattern using swift closures",
    "homepage": "https://github.com/jameslintaylor/mime",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "authors": "James Taylor",
    "social_media_url": "http://twitter.com/jameslintaylor",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/jameslintaylor/mime.git",
        "tag": "1.0.0-beta.1"
    },
    "source_files": "mime/*.swift",
    "frameworks": "UIKit",
    "dependencies": {
        "AssociatedObjects": [
            "~> 1.0.0-beta.2"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This