awalz / swiftydraw Goto Github PK
View Code? Open in Web Editor NEWA simple, lightweight drawing framework written in Swift
License: MIT License
A simple, lightweight drawing framework written in Swift
License: MIT License
Hi,
Love this framework! Quick question, is there a way to know the sample rate of touches it picks up or adjust that rate? Thanks!!
Karunya
Just like in shapes like eclipse, we can fill the shape and add stroke to that shape with any color, how can we achieve this while being in draw mode?
I just want that whatever I draw, it should have a stroke and a fill of different colours. Kind of like a boundary around whatever I draw.
I'm considering adding a property that, if set, will only draw if the screen is touched with Apple Pencil. (It would be as easy as checking the source of the touch)
Also, we could detect the event from Apple Pencil (double tap) to switch the tools currently being used.
What do you think of the idea? I can work on this.
Hello, Linus! Thank you for improving the best drawing framework for iOS! Is it possible to add the single dots drawing support? Currently if you do single taps in the sample project, nothing happens. Anton
The Readme ays this is Swift 5 compatible, but it does not compile with Swift 5 through Carthage. The build asks to convert to Swift 5, so it looks like a new Swift 5 release is needed.
Value of type 'SwiftyDrawView' has no member 'getCurrentPathArray'
and
Value of type 'SwiftyDrawView' has no member 'setCurrentPathArray'
from this line of code
if self.signature != nil { self.signatureCanvas.setCurrentPathArray((self.signature?.getCurrentPathArray())!) }
SwiftyDraw 2.3
iOS 13.5
Xcode 11.5
Swift 4.2
I love SwiftyDraw. Can you please update cocoapods to version 2.0?
I've updated the touchesBegan
function in my forked repo to experiment adding a 'continue path' functionality, where the line you draw will automatically continue from the last one you did (if it exists)
Is something like this worth adding into the library?
The updated touchesBegan()
looks like
/// Determines whether the path should continue from the last touch point.
public var shouldContinuePath = false
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard isEnabled, let touch = touches.first else { return }
if #available(iOS 9.1, *) {
guard allowedTouchTypes.flatMap({ $0.uiTouchTypes }).contains(touch.type) else { return }
}
guard delegate?.swiftyDraw(shouldBeginDrawingIn: self, using: touch) ?? true else { return }
delegate?.swiftyDraw(didBeginDrawingIn: self, using: touch)
firstPoint = shouldContinuePath && previousPoint != .zero ? previousPoint : touch.location(in: self)
setTouchPoints(touch, view: self)
let newLine = DrawItem(path: CGMutablePath(),
brush: Brush(color: brush.color.uiColor, width: brush.width, opacity: brush.opacity, blendMode: brush.blendMode), isFillPath: false)
if shouldContinuePath && currentPoint != .zero {
// draw a straight line between the end of the last touch point
// and this new touch point
newLine.path.addPath(createNewStraightPath())
}
addLine(newLine)
}
The #40 PR added an ability to encode/decode draw data, but appropriate function has "internal" protection level by default. While trying to use it, Xcode says 'addLine' is inaccessible due to 'internal' protection level
, so if I install library via CocoaPods or other dependency manager, it's impossible to use addLine()
without manual source code editing.
We just need to mark it as "open".
Hi sir. Im sorry if I disturb you. But this what problem I encountered and I don't know if this relates to your pod.
This view with grey background is my sketch view (a view inside view controller)
The problem here is that when I draw it overlaps to my sketchview, and i can't draw in the upper portion of the sketchview (please just look at the screenshot)
Is there a possibility to get support for soft brushes? (progressive opacity, solid inside, more clear on edges). I tried to do it with a combination of context.setShadow(...)
and multiple drawings of the same path, but this is not perfect. I couldn't do it via "stroking" bitmap, tried everything really.
Hi I'm trying to make your view rotate & zoom.
For the zooming part I can implement the gesture in the viewDidLoad :
let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:))) self.drawView.addGestureRecognizer(gesture)
and adding this function :
@objc func pinchAction(sender:UIPinchGestureRecognizer) { let scale:CGFloat = previousScale * sender.scale self.view.transform = CGAffineTransform(scaleX: scale, y: scale); previousScale = sender.scale }
var previousScale:CGFloat = 1.0
but my strategy isn't working for the rotation (or translation ?) since it's not detecting this as a proper rotation event ?
Do you have any idea how to fix this ?
Thanks for you repo & awesome work by the way !
Could you add new version tag and update version in pod file to make latest updates available? Thnx
Thanks for this! I dragged SwiftyDraw to my project. I added this to viewDidLoad():
let drawView = SwiftyDrawView(frame: self.view.frame)
self.view.addSubview(drawView)
I also made the UIViewController a SwiftyDrawViewDelegate. None of the delegate methods (began, ended) are called.
Is there a way to convert the drawing of a SwiftyDraw view to a UIImage? Is there like a property I can access like drawView.image
or drawView.saveImage()
, or some other method by accessing its contents (I know there a lines property) and converting this into a UIImage? Thanks in advance.
I set the drawView background color to orange. I change blend Mode is 'clear' after I draw. The erase part can not have an orange background. How can I fix it?
Hello! Sorry i dont know if this is an issue, or im just wrong on using it (sorry, im new at ios dev)
@IBAction func markerButtonPressed(_ sender: Any) {
drawView.brush.color = .red
drawView.brush = Brush.thick
}
The problem is that when I clicked the button, it doesn't change the color of the line.
How are you?
I am going to draw line with image pattern.
How can I do for this?
Could you guide me how can I do?
Thank you.
Apart from Clear Canvas, can you add eraser functionality as well?
Hello! I faced FPS drop on the draw() method, if a lot of lines will be in the drawing bounds. Any ideas about it?
Starting from the first touch point it should draw a straight line to the end point when touches end.
Hi ,
I did see this
it worked to get an image with the background space
for example, if the UIView is 200X200
and I did draw in the corner
when I get the image will be 200X200
with all the empty space and with the drawing I did in the corner
I want the get only the draw without the empty spaces
before I use this library I was using
YPDrawSignatureView
and it did it like this
// Save the Signature (cropped of outside white space) as a UIImage
public func getCroppedSignature(scale:CGFloat = 1) -> UIImage? {
guard let fullRender = getSignature(scale:scale) else { return nil }
let bounds = self.scale(path.bounds.insetBy(dx: -strokeWidth/2, dy: -strokeWidth/2), byFactor: scale)
guard let imageRef = fullRender.cgImage?.cropping(to: bounds) else { return nil }
return UIImage(cgImage: imageRef)
}
I did try to do a similar thing with this library but with no luck
We believe the source needs an update supporting xcode 14+
Much appreciated if you can have a lot so we can update our application.
Hello, guys.
Can you allow initialise Line out of library? It necessary for exit and returning to working on the picture. Just this.
public init(path: CGMutablePath, brush: Brush) { self.path = path self.brush = brush }
Andrew, hello!
Just a quick question - I am trying to change colour of a line, however, whenever I press on a button Xcode throws a breakpoint and says - "unexpectedly found nil while unwrapping an Optional value" on a line of drawView.lineColor= button.color
This error is always pretty easy to solve but now I cannot find a solution to it... Here is my piece of code:
class PhotoViewController: UIViewController, SwiftyDrawViewDelegate {
private var backgroundImage: UIImage?
var drawView : SwiftyDrawView!
var cancelButton: UIButton!
var shareButton: UIButton!
var drawButton: UIButton!
var saveButton: UIButton!
var doneButton: UIButton!
var redButton : ColorButton!
init(image: UIImage) {
self.backgroundImage = image
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.gray
let backgroundImageView = UIImageView(frame: view.frame)
backgroundImageView.contentMode = .scaleAspectFill
backgroundImageView.image = backgroundImage
view.addSubview(backgroundImageView)
addButtons()
}
func addButtons() {
cancelButton = UIButton(frame: CGRect(x: 10.0, y: 10.0, width: 50.0, height: 50.0))
cancelButton.showsTouchWhenHighlighted = true
cancelButton.setImage(#imageLiteral(resourceName: "Close"), for: UIControlState())
cancelButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
view.addSubview(cancelButton)
saveButton = UIButton (frame: CGRect(x: 16.0, y: 503.0, width: 50, height: 50))
saveButton.showsTouchWhenHighlighted = true
saveButton.setImage(#imageLiteral(resourceName: "Save"), for: UIControlState())
saveButton.addTarget(self, action: #selector(save), for: .touchUpInside)
view.addSubview(saveButton)
shareButton = UIButton (frame: CGRect(x: 240.0, y: 484.0, width: 60, height: 60))
shareButton.showsTouchWhenHighlighted = true
shareButton.setImage(#imageLiteral(resourceName: "Share"), for: UIControlState())
shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
view.addSubview(shareButton)
drawButton = UIButton (frame: CGRect(x: 260.0, y: 10.0, width: 50, height: 50))
drawButton.showsTouchWhenHighlighted = true
drawButton.setImage(#imageLiteral(resourceName: "Draw"), for: UIControlState())
drawButton.addTarget(self, action: #selector(draw), for: .touchUpInside)
view.addSubview(drawButton)
}
func draw() {
let drawView = SwiftyDrawView(frame: self.view.frame)
drawView.delegate = self
self.view.addSubview(drawView)
let cancelButton = UIButton(frame: CGRect(x: 10.0, y: 10.0, width: 50.0, height: 50.0))
cancelButton.showsTouchWhenHighlighted = true
cancelButton.setImage(#imageLiteral(resourceName: "Close"), for: UIControlState())
cancelButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
view.addSubview(cancelButton)
let doneButton = UIButton(frame: CGRect(x: 260.0, y: 10.0, width: 50.0, height: 50.0))
doneButton.showsTouchWhenHighlighted = true
doneButton.setImage(#imageLiteral(resourceName: "Done"), for: UIControlState())
doneButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
view.addSubview(doneButton)
let redButton = ColorButton(frame: CGRect(x: 10, y: self.view.frame.height - 50, width: 40, height: 40), color: UIColor.red)
redButton.showsTouchWhenHighlighted = true
redButton.addTarget(self, action: #selector(colorButtonPressed(button:)), for: .touchUpInside)
self.view.addSubview(redButton)
UIView.animate(withDuration: 0.1, animations: {
self.cancelButton.alpha = 0.0
self.shareButton.alpha = 0.0
self.saveButton.alpha = 0.0
self.drawButton.alpha = 0.0
})
}
func colorButtonPressed(button: ColorButton) {
drawView.lineColor = button.color
}
func SwiftyDrawDidBeginDrawing(view: SwiftyDrawView) {
}
func SwiftyDrawIsDrawing(view: SwiftyDrawView) {
}
func SwiftyDrawDidFinishDrawing(view: SwiftyDrawView) {
}
func SwiftyDrawDidCancelDrawing(view: SwiftyDrawView) {
}
Also, what would be your advice on merging background image with the drawing?
Thank you, Andrew!
Can you add different shapes especially Triangle & Hexagon?
When building with Xcode 14 the following build error is encountered for allowedTouchTypes
and pencilInteraction
.
Stored properties cannot be marked potentially unavailable with '@available'
Since Xcode 14 drops support for iOS 10 and below, just removing @available
from allowedTouchTypes
should be just fine.
How to add image as background ?
How can I save the drawing to a database and later on give the possibility to edit or add new content to the same drawing?
Hi, thank you for providing this excellent library. I encountered a problem while using it: how can I disable the drag and zoom events of UIScrollView when adding a subview SwitfyDraw to it and drawing while scrolling?
I am new to UIKit, so the problem I described is a bit vague. Thank you for your help.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.