🎉 MaLiang is on Metal now!
MaLiang is a painting framework based on Metal. The name of "MaLiang" comes from a boy who had a magical brush in Chinese ancient fairy story.
iOS 9.0+, Swift 4.2+
The core painting module is based on Metal
You can simply make it compatible with lower version of iOS and swift by changing only serval lines of code.
MaLiang is available through CocoaPods. To install it, simply add the following line to your Podfile:
# 2.0.0 is currently in beta testing
pod 'MaLiang', '~> 2.0.0-beta-2'
To use the old OpenGL ES verion:
pod 'MaLiang', '~> 1.1'
MaLiang is simple to use.
- import MaLiang
- enjoy painting!
open class Canvas: MetalView
A Canvas
is the basic component of MaLiang
. You will paint all things on it.
Canvas
extends from MetalView
, whitch extends from MTKView
. MetalView
handles all the logic with MetalKit and hides them from you.
Canvas
can be simply created with xib or code.
- with xib or storyboard, simply drag and drop an
UIView
object into your view controller and change it's class toCanvas
and module toMaLiang
- with code, just create with
init(frame:)
as anyUIView
you do before.
Now, all things necessary is done!
You can take snapshot on canvas now. Just call snapshot
function on Canvas
and you will get an optional UIImage
object.
With all things done, you can do more with Brush
!
Brush
is the key feature to MaLiang
. It holds textures and colors, whitch makes it possiable to paint amazing things.
Register a Brush
with image data or file to Canvas and paint with it:
let path = Bundle.main.path(forResource: "pencil", ofType: "png")!
let pencil = try? canvas.registerBrush(with: URL(fileURLWithPath: path))
pencil?.use()
Brush
have serval properties for you to custmize:
// opacity of texture, affects the darkness of stroke
// set opacity to 1 may cause heavy aliasing
open var opacity: CGFloat = 0.3
// width of stroke line in points
open var pointSize: CGFloat = 4
// this property defines the minimum distance (measureed in points) of nearest two textures
// defaults to 1, this means erery texture calculated will be rendered, dictance calculation will be skiped
open var pointStep: CGFloat = 1
// sensitive of pointsize changed from force, from 0 - 1
open var forceSensitive: CGFloat = 0
/// color of stroke
open var color: UIColor = .black
With all these properties, you can create you own brush as your imagination.
MaLiang supports automatically adjustment of stroke size with painting force. 3D Touch is supported by default, and simulated force will be setup on devices those are not supporting this.
forceSensitive
is the property that force affects the storke size. It should be set between 0
to 1
. the smaller the value is, the less sensitive will be. if sets to 0
, then force will not affects the stroke size.
Document
is now configured by default. It holds all the data on the Canvas
, and makes the undo and redo actions to be possiable.
And you can implement your own saving logic with the data holds by Document
.
- Port to Metal
- Undo & Redo
- Export to image
- Optimize for scale
- Optimize for saving logic
- Glow style line
- Text element
- Image element
- Texture rotation
MaLiang is available under the MIT license. See the LICENSE file for more info.