Code Monkey home page Code Monkey logo

polygonviewdemo's Introduction

Swift Polygon View using CAShapeLayer

This is an demonstration of using CAShapeLayer/UIBezierPath to create a view that contains a polygon in Swift.

The basic idea is to create a CAShapeLayer and add it to the CALayer hierarchy once, but then update the path for that shape layer whenever the view changes size (either in layoutSubviews in UIView class or viewDidLayoutSubviews in UIViewController). Personally, I like to do this stuff as an @IBDesignable view subclass, like I have here, because that way I can use it in a storyboard (just drag a a simple "View" object onto the scene and then set its base class to be PolygonView (or whatever you call it here). The only trick with using designables is that Apple advises putting them in a separate framework target, like I have here.

But none of this IBDesignable stuff is critical to the question at hand, namely just use CAShapeLayer and update its path when the view changes size.

This is not intended as an end-user library, but this is for illustrative purposes only.

See http://stackoverflow.com/a/41476105/1271826.

Developed in Swift 3 on Xcode 8.2.1 for iOS 10. But, the basic ideas are equally applicable for different versions of Swift and/or Objective-C.

License

Copyright © 2017 Robert Ryan. All rights reserved.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

--

16 January 2017

polygonviewdemo's People

Contributors

robertmryan avatar

Watchers

 avatar  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.