Introduction
RoughSwift is a library that lets you draw and animate hand-drawn, sketchy, or rough-looking shapes and lines in your Swift applications. It provides an easy-to-use API for creating captivating and unique visuals.
Features
- Create hand-drawn circles, rectangles, polygons, and lines
- Apply stylistic roughness and sketchiness to shapes
- Animate the drawing and appearance of shapes
- Customize stroke color, fill color, and line width
- Support for touch events and interactions
Getting Started
To start using RoughSwift in your project, follow these steps:
- Install Rough by running the following command in your terminal:
- Import RoughSwift into your Swift file:
- Create a new RoughView object:
pod 'RoughSwift'
import RoughSwift
let roughView = RoughView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
Basic Usage
To draw a hand-drawn shape using RoughSwift, follow these steps:
- Create a new RoughDrawable object for the desired shape:
- Set the desired style for the shape:
- Add the shape to the RoughView:
let roughShape = RoughDrawable.createCircle(center: CGPoint(x: 100, y: 100), radius: 50)
roughShape.strokeColor = UIColor.red
roughView.add(shape: roughShape)
Animations
RoughSwift provides built-in animations for drawing and animating the appearance of shapes. To animate a shape, use the following methods:
- startDrawing(duration: TimeInterval): Starts animating the drawing of the shape with the specified duration.
- startDrawingWithCompletion(duration: TimeInterval, completion: (() -> Void)? = nil): Starts animating the drawing of the shape, optionally providing a completion closure to be executed once the animation finishes.
- startAnimation(duration: TimeInterval): Animates the appearance of the shape, making it transition from a rough sketch to a finalized form with the specified duration.
- startAnimationWithCompletion(duration: TimeInterval, completion: (() -> Void)? = nil): Animates the appearance of the shape, with an optional completion closure to be executed once the animation finishes.
Advanced Usage
RoughSwift offers additional functionalities to enhance your hand-drawn visuals:
- RoughStyle: Customize the roughness of a shape using options like roughness, bowing, fill, and fillWeight.
- RoughInteraction: Enable touch interactions on shapes to create interactive experiences.
- RoughGroup: Group multiple shapes together for easier manipulation and animation.
Examples
Find below some examples demonstrating the capabilities of RoughSwift:
- Creating a rough rectangle:
- Animating a hand-drawn line:
let roughRectangle = RoughDrawable.createRectangle(rect: CGRect(x: 50, y: 50, width: 100, height: 200))
roughRectangle.strokeColor = UIColor.blue
roughView.add(shape: roughRectangle)
let roughLine = RoughDrawable.createLine(start: CGPoint(x: 50, y: 100), end: CGPoint(x: 150, y: 100))
roughLine.strokeColor = UIColor.green
roughView.add(shape: roughLine)
roughLine.startDrawing(duration: 2.0)
Conclusion
RoughSwift allows you to unleash your creativity by adding hand-drawn aesthetics to your applications. With its intuitive API and powerful capabilities, you can create visually appealing and unique visuals that engage your users.