Git Product home page Git Product logo

uibezierpath-superpowers's Introduction

UIBezierPath+Superpowers

A one-file extension library to add several cool features to UIBezierPath.

Getting Started

This library consists of one single file. Gettings started is as easy as copying this file to your Xcode project.

Prerequisites

Developed with Xcode 9 and Swift 4. Tested with Swift 3+. Works with Xcode 8 with some minor adjustments.

Xcode 8+
Swift 3+
iOS 8+

Installation

Copy UIBezierPath+Superpowers.swift to your project and you're good to go.

Demo

The repo includes a demo project which lets you try out the features with different predefined paths. Feel free to add your own svg files and extend the Demo enum respectively.

Note: The demo project was created with Xcode 9 and works with iOS9+.


Features

This library adds several major features to UIBezierPath.

Calculating path length

var mx_length: CGFloat

Returns the length of the path.

Calculating bezier points

mx_point(atFractionOfLength: CGFloat) -> CGPoint

Returns for a given fraction the point on the path fraction * pathLength in to the path.

Calculating perpendicular points and distances

mx_perpendicularPoint(for: CGPoint) -> CGPoint

Returns the closest point on the path to a given CGPoint, effectively letting fall a perpendicular on the path from point and returning the point of intersection.

Calculating path slope and tangent angles

mx_tangentAngle(atFractionOfLength: CGFloat) -> CGFloat

For a given fraction, returns the tangent angle of the path at the point fraction * length in to the path.


How it works

The backbone of it all is a function on CGPath called apply(info:, function:). This basically lets you iterate all elements a given UIBezierPath is constructed of.

Short aside:

UIBezierPath and its Cocoa counterpart NSBezierPath are descriptions of bezier curves. These curves are basically a clever way of feeding line drawing instructions to computers. Bezier paths are composed of different types of path elements. Each element is either a straight line, quadratic curve or cubic curve. At the end of the day, these curves boil down to mathematical functions which allows us to calculate things like line lengths, tangent angles, etc. If you are interested in more, there is a really well written and easy to understand article on Wikipedia.

What this library does is pull out all elements a path is constructed of and throw some geometric math at them. Since the extraction and initial computations are quite expensive, the results are cached internally if possible.

By default however, caching is disabled because - by design - the library lives in an extension and not a subclass of UIBezierPath and with that come some limitations regarding method overriding, internal vars, etc. In order to enable the full superpower, you'll have to call UIBezierPath.mx_prepare(). This performs some runtime magic under the hood which breaks the chains of extension and gives you full access to all features on any UIBezierPath object.

Contributing

Feel free to build upon this project and / or submit a PR anytime.


Authors

  • Maximilian Kraus

License

This project is licensed under the MIT License - see the LICENSE.md file for details


Acknowledgments

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.