Giter Site home page Giter Site logo

tactile's Introduction

Travis Status CocoaPods compatible Carthage compatible

Tactile is a safer and more idiomatic way to respond to gestures and control events. It lets you catch bugs at compile time and write more expressive code.

view.pan([
    .began:   panBegan,
    .changed: panChanged,
    .ended:   panEnded
])

// func panBegan(pan: UIPanGestureRecognizer)
// func panChanged(pan: UIPanGestureRecognizer)
// func panEnded(pan: UIPanGestureRecognizer)

UsageInstallationLicense

Usage

Tactile extends both UIView and UIControl classes.

UIView extensions

UIView+Tactile.swift

The on method

Use the on method to add gesture recognizers.

on(gesture:callback:)

let tap = UITapGestureRecognizer()
tap.numberOfTapsRequired = 3
tap.numberOfTouchesRequired = 2

view.on(tap, tapped)

// func tapped(tap: UITapGestureRecognizer)

on(gesture:state:callback:)

let pinch = UIPinchGestureRecognizer()

view.on(pinch, .began, pinchBegan)

// func pinchBegan(pinch: UIPinchGestureRecognizer)

on(gesture:states:callback:)

let pan = UIPanGestureRecognizer()

view.on(pan, [.began, .ended], panBeganOrEnded)

// func panBeganOrEnded(pan: UIPanGestureRecognizer)

on(gesture:callbacks:)

let pinch = UIPinchGestureRecognizer()

view.on(pinch, [
  .began: pinchBegan,
  .ended: pinchEnded
])

// func pinchBegan(pinch: UIPinchGestureRecognizer)
// func pinchEnded(pinch: UIPinchGestureRecognizer)

The shorthand methods

Tactile defines 6 shorthand methods: longPress, pan, pinch, rotation, swipe and tap.

<shorthand>(callback:)

view.tap(tapped)

// func tapped(tap: UITapGestureRecognizer)

<shorthand>(state:callback:)

view.pinch(.began, pinchBegan)

// func pinchBegan(pinch: UIPinchGestureRecognizer)

<shorthand>(states:callback:)

view.pan([.began, .ended], panBeganOrEnded)

// func panBeganOrEnded(pan: UIPanGestureRecognizer)

<shorthand>(callbacks:)

view.longPress([
  .began: longPressBegan,
  .ended: longPressEnded
])

// func longPressBegan(longPress: UILongPressGestureRecognizer)
// func longPressEnded(longPress: UILongPressGestureRecognizer)

The off method

Use the off method to remove gesture recognizers.

off(gesture:)

let tap = UITapGestureRecognizer()
view.on(tap, tapped)

// ...

view.off(tap)

off(gestureType:)

view.off(UITapGestureRecognizer.self)

off()

view.off()

Attaching a gesture recognizer to multiple views

With Tactile, you can attach the same gesture recognizer to multiple views.

let tap = UITapGestureRecognizer()
tap.numberOfTapsRequired = 3
tap.numberOfTouchesRequired = 2

firstView.on(tap, firstViewTapped)
secondView.on(tap, secondViewTapped)

UIControl extensions

UIControl+Tactile.swift

Use the on method to attach an event handler function for one or more control events.

on(event:callback:)

button.on(.touchUpInside, tapped)

// func tapped(button: UIButton)

on(events:callback:)

button.on([.touchUpInside, .touchUpOutside], tapped)

// func tapped(button: UIButton)

on(callbacks:)

button.on([
  .touchUpInside: tapped,
  .touchUpOutside: cancelledTap
])

// func tapped(button: UIButton)
// func cancelledTap(button: UIButton)

Installation

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Tactile into your Xcode project using Carthage, specify it in your Cartfile:

github "delba/Tactile" >= 1.0

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

You can install it with the following command:

$ gem install cocoapods

To integrate Tactile into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

pod 'Tactile', '~> 1.0'

License

Copyright (c) 2015-2019 Damien (http://delba.io)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

tactile's People

Contributors

colinta avatar delba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tactile's Issues

Using Tactile with UIControl works inconsistently

I like the concept very much, unfortunately Tactile isnt reliable. When using it as so:

let button = UIButton()
button.on(.TouchUpInside, functionToCall)

it only works about 95% of the time for me and my colleagues on different projects.

I had a quick look and would like to point you to this piece of code:

116 private let proxies = NSMapTable.weakToStrongObjectsMapTable()
117 
118 private class Proxy: NSObject {
119     var actor: Triggerable!
120     
121     init(actor: Triggerable, control: UIControl, event: UIControlEvents) {
122         super.init()
123         
124         self.actor = actor
125         
126         proxies.setObject(self, forKey: "\(control, event.rawValue)")
127     }
128     
129     @objc func recognized(control: UIControl) {
130         actor.trigger(control)
131     }
132 }

You are using a weakToStrongObjectsMapTable(), which has the property that: "Use of weak-to-strong map tables is not recommended. The strong values for weak keys which get zeroed out continue to be maintained until the map table resizes itself."
I think that the key "\(control, event.rawValue)" goes out of scope immediately, then, whenever the mapTable is resized, the values are released and stop working.

Also suspicious to me is the cycle Actor<--owns-->Proxy.

Can you please take a look at this and explain? Thanks

Testing Tactile methods

Hi! What is the best way to test logic passed to .tap() or .pinch() methods? How to programmatically trigger those callbacks?

Block support

Hello,

first, very nice work indeed. What would help this project to be really great is if the methods offered block version instead of target. That way you would be able to do following:

view.on(tap) {
    // Do something
}

Which removes the decoupling - the need to have different method somewhere else in the code.

Thanks for your time doing this library!

Sincerely,
Jiri

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.