Giter Site home page Giter Site logo

drawaction's Introduction

DrawAction Carthage compatible Build Status

DrawAction is a simple swift library for composing drawing operations in iOS.

Usage

The DrawAction class is the abstract superclass for every action. It defines the base ability to add additional actions to the end of the current chain, and to initiate drawing by supplying a CGRect and a CGGraphicsContext.

Actions

The different available actions can be thought of as belonging to one of two categories: Graphics Actions and Composing Actions

Graphics Actions

These actions do the actual drawing, and don't affect the state of drawing other than whatever action is being performed. This includes actions like DrawBorder, DrawFill, DrawLine, etc.

Composing Actions

These actions change the context of later actions. This includes changing the current rect or path future actions can use, or creating a separate branch of drawing actions in the chain. This includes actions like DrawInset, DrawPath, DrawSplit etc.

Refer to the comments in the source for further information on specific actions

Examples

Fill a blue rect with a red border

DrawFill(color: UIColor.blueColor()).add(DrawBorder(color: UIColor.redColor())).drawRect(rect, inContext: UIGraphicsGetCurrentContext())

Fill two rects next to each other, one with a shadow the other without.

let leftRect = DrawFill(color: UIColor.blueColor())
let rightRect = DrawShadow(color: UIColor.blackColor(), blur: 2, offset: CGSize(width: -1, height: 1)).add(DrawFill(color: UIColor.redColor()))
DrawDivide(amount: rect.width/2 - 5, padding: 10, edge: .MinXEdge, slice: leftRect, next: rightRect).drawRect(rect, inContext: UIGraphicsGetCurrentContext())

Draw a rounded rect with a border gradient, background gradient and shadow, with "Hello world" printed in the middle with a shadow

let backgroundAction = DrawAction.chainActions([
        DrawShadow(color: UIColor.blackColor(), blur: 1, offset: CGSizeMake(0, 1)),
        DrawTransparency(), // All subsequent drawing operations will be in a separate 'transparency layer', so the shadow will be applied even for the gradient
        DrawInset(uniformInset: 2),
        DrawPath(roundedRectRadius: 5),
        DrawClip(), // Gradients don't respect context paths by default so we clip to the path
        DrawGradient(colors: [UIColor.lightGrayColor(), UIColor.darkGrayColor()], horizontal: true),
        DrawBorderGradient(colors: [UIColor.darkGrayColor(), UIColor.lightGrayColor()], lineWidth: 4, inset: 0)
])

// White text with shadow
let textAction = DrawShadow(color: UIColor.blackColor(), blur: 0, offset: CGSizeMake(1, 1)).add(
        DrawText(text: "Hello World", font: UIFont.preferredFontForTextStyle(UIFontTextStyleBody), color: UIColor.whiteColor(), alignment: .Center, lineBreakMode: .ByTruncatingTail))

let drawSize = CGSize(width: 100, height: 30)
let drawRect = CGRect(origin: CGPoint(x: rect.midX - drawSize.width/2, y: rect.midY - drawSize.height/2), size: drawSize)

DrawSplit(split: backgroundAction, next: textAction).drawRect(drawRect, inContext: UIGraphicsGetCurrentContext())

To see these examples in action, and to test other actions, run the DrawActionExample app in the project

License

DrawAction is available under the MIT license. See the LICENSE file for more info.

drawaction's People

Contributors

bobeldevil avatar earlyshen avatar jtschult avatar skovatch avatar traviswestbrook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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