Giter Site home page Giter Site logo

jason-cooke / standard-template-protocols Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cconeil/standard-template-protocols

0.0 1.0 0.0 693 KB

Protocols for your every day iOS needs

License: MIT License

Swift 95.50% Objective-C 1.89% Ruby 2.61%

standard-template-protocols's Introduction

Standard Template Protocols

Essential protocols for your every day iOS needs

language MIT License Platform

Example

UIGestureRecognizerProtocols

About

Swift 2.0 opens a world of opportunity with enhanced Protocols and Protocol Extensions. This library attempts to address some of the most commonly repeated patterns in iOS apps using protocol oriented programming and reduce the need to create deep, complicated subclassing trees.

Why Protocols?

Too often we find ourselves locked into deep and complicated subclassing trees just to factor out common behavior in our apps. This makes our code inflexible, hard to navigate, and contain too many dependencies. Using protocols for common features allows us to create default behavior that is additive without complicated subclassing.

Setup

CocoaPods (recommended)

  1. Make sure you have downloaded and installed cocoapods.
  2. Add pod 'STP', '~> 0.3.0'to your podfile.
  3. Make sure that your podfile includes use_frameworks!
  4. pod install
  5. import STC and let the magic happen!

Manual

  1. Drag and drop the files into your project and build the app. Note you will not get bugfixes, updates, and new releases if you do this. If you want custom features, I reccommend forking this repo.

UIGestureRecognizer Protocols

All too often, we find ourselves subclassing views to allow them to be tappable, moveable, rotatable, and more. These protocols allow you to add these features by simply conforming to a protocol, but still give you the flexible to create custom features and animations.

Moveable

By default, making a view conform to the Movable protocol will attach a UIPanGestureRecognizer and allow the user to tap on the view and drag it around the screen. The default behavior is for the view to only be dragged within its superview. Creating a moveable view is as simple as:

class MyMoveableView : UIView, Moveable {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.makeMoveable()
    }
}

To do an action on start or finish, or use custom logic for movement or animation, implement the appropriate methods in the Moveable protocol.

func didStartMoving()
func didFinishMoving(velocity:CGPoint)
func canMoveToX(x:CGFloat) -> Bool
func canMoveToY(y:CGFloat) -> Bool
func translateCenter(translation:CGPoint, velocity:CGPoint, startPoint:CGPoint, currentPoint:CGPoint) -> CGPoint
func animateToMovedTransform(transform:CGAffineTransform)

Pinchable

By default, making a view conform to the Pinchable protocol will attach a UIPinchGesetureRecognizer and allow the user to pinch and scale a view. Creating a pinchable view is as simple as:

class MyPinchableView : UIView, Pinchable {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.makePinchable()
    }
}

To do an action on start or finish, or create custom transform or animation logic, simply implement the appropriate methods in the Pinchable protocol

func didStartPinching()
func didFinishPinching()
func maximumPinchScale() -> CGFloat
func minimumPinchScale() -> CGFloat
func transformWithScale(scale:CGFloat, lastScale:CGFloat, velocity:CGFloat) -> CGAffineTransform
func animateToPinchedTransform(transform:CGAffineTransform)

Rotatable

By default, making a view conform to the Rotatable protocol will attach a UIRotationGestureRecognizer and allow the user to use two fingers to rotate a view. Creating a rotatable view is as simple as:

class MyRotatableView : UIView, Rotatable {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.makeRotatable()
    }
}

To do an action on start or finish, or create custom transform or animation logic, simply implement the appropriate methods in the Rotatable protocol

func didStartRotating()
func didFinishRotating(velocity:CGFloat)
func minimumRotation() -> CGFloat
func maximumRotation() -> CGFloat
func transformWithRotation(rotation:CGFloat, lastRotation:CGFloat, velocity:CGFloat) -> CGAffineTransform
func animateToRotatedTransform(transform:CGAffineTransform)

Tappable

By default, making a view conform to the Tappable protocol will attach a UILongPressGestureRecognizer and allow the user to tap. It will call the didTap() method and set the alpha of the view to 0.5 on the down state and 1.0 on the up state. Creating a tappable view is as simple as:

class MyTappableView : UIView, Tappable {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.makeTappable()
    }
    
    func didTap() {
         print("tapped!")
    }
}

To do customize the up and down state and/or adjust the minimum press duration and allowable movement, simply implement the appropriate methods in the Tappable protocol.

func didTap()
func didTouchDown()
func didTouchUp()
func minimumPressDuration() -> NSTimeInterval
func allowableMovement() -> CGFloat

Forceable

By default, making a view conform to the Forceable protocol will attach a ForceTouchGestureRecognizer and allow the user to press down with a force. It will call the didForce() method, but doesn't have any default behavior. Creating a forceable view is as simple as:

class ForceView: UIView, Forceable {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.makeForceable()
    }
}

To do customize the vide on force, simply implement the appropriate methods in the Forceable protocol.

func didStartForcing(force:CGFloat)
func didForce(force:CGFloat, lastForce:CGFloat)
func didFinishForcing(force:CGFloat)

Using Them Together

Because protocols are addative, you can mix and match these protocols to create even more dynamic views. Creating a view that movable, pinchable, rotatable, and tappable is as easy as:

class MyAwesomeView : UIView, Moveable, Rotatable, Pinchable, Tappable {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.makeMoveable()
        self.makeRotatable()
        self.makePinchable()
        self.makeTappable()
    }
}

Contributions

Pull requests are welcome! :) You can also email me or contact me on Twitter if you have any questions, ideas, or just want to contribute.

t: @chrisoneil_

e: [email protected]

A special thanks to jhurray for inspring me to do work on some open source code.

TODO:

There's a ton that we can do here, and I would love to hear suggestions and get pull requets. Here are some things that I'm planning to work on in the immediate future.

  1. A protocol for view controllers that implements default peek and pop for force touch.
  2. A protocol for view controllers that drops down an error banner.

License

Standard Template Protocols is released under the MIT license. See LICENSE for details.

standard-template-protocols's People

Contributors

cconeil avatar crenwick avatar jhurray avatar readmecritic avatar

Watchers

James Cloos avatar

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.