Draw a line with UIBezierPath
Ended up doing it this way:
func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
//design the path
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
//design path in layer
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.CGColor
shapeLayer.lineWidth = 1.0
view.layer.addSublayer(shapeLayer)
}
Drawing a line in Swift 3.x with UIBezierPath
I use this to draw a line:
let doYourPath = UIBezierPath(rect: CGRect(x: xPos, y: yPos, width: yourWidth, height: yourHeight))
let layer = CAShapeLayer()
layer.path = doYourPath.cgPath
layer.strokeColor = UIColor.white.cgColor
layer.fillColor = UIColor.white.cgColor
self.view.layer.addSublayer(layer)
Hope this help you out. This is just one the way to draw a line in swift.
How to stretch a line which is drawn using UIBezierPath?
Try using properties to store the points you want to change and recreate the path when needed.
class FileViewController: UIViewController {
let line = CAShapeLayer()
var point1 = CGPoint.zero
var point2 = CGPoint.zero
func DrawLine() {
point1 = CGPoint(x: 100, y: 100)
point2 = CGPoint(x: self.view.frame.width - 100, y: 100)
let linePath = UIBezierPath()
linePath.move(to: point1)
linePath.addLine(to: point2)
line.path = linePath.cgPath
line.strokeColor = UIColor.red.cgColor
line.lineWidth = 1
line.lineJoin = kCALineJoinRound
self.view.layer.addSublayer(line)
}
override func viewDidLoad() {
super.viewDidLoad()
DrawLine()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
point1.x -= 10
point2.x += 10
let linePath = UIBezierPath()
linePath.move(to: point1)
linePath.addLine(to: point2)
line.path = linePath.cgPath
}
}
draw line using uibezierPath in a vertical way
Actually you move on to startPoint and trying to add line to the same static point ... how it can add line to the same point on which you are currently residing ... add some value to Y
while static X
position position to add line
bezier.move(to: startPoint!)
bezier.addLine(to: CGPoint(x: startPoint!.x, y: point.y))
How to move a line created with UIBezierPath?
This is because you are not starting the line you are drawing from CGPoint.zero
. Change
linePath.move(to: to: CGPoint(x: 100, y: 100)
linePath.addLine(to: CGPoint(x: self.view.frame.width - 100, y: 100))
to
linePath.move(to: .zero)
linePath.addLine(to: CGPoint(x: 100, y: 0))
how can i draw straight line using UIBezierPath in ios
Try this:
@implementation View
{
NSMutableArray paths;
UIBezierPath *currentPath;
CGPoint startPoint;
}
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO]; // (2)
[self setBackgroundColor:[UIColor whiteColor]];
paths = [NSMutableArray array];
}
return self;
}
- (void)drawRect:(CGRect)rect // (5)
{
[[UIColor blackColor] setStroke];
for(UIBezierPath *path in paths) {
[path stroke];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
startPoint = [touch locationInView:self];
currentPath = [UIBezierPath bezierPath];
[currentPath setLineWidth:5.0];
[paths addObject:currentPath];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[currentPath removeAllPoints];
[currentPath moveToPoint:startPoint];
[currentPath addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
Erase UIBezierPath
I managed to solve this using CGContext
.
@objc private func didPanWhiteboard(_ gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began:
drawingState = .began
lastPoint = gesture.location(in: drawingBoardImageView)
case .changed:
drawingState = .moved
let currentPoint = gesture.location(in: drawingBoardImageView)
drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
case .cancelled, .ended:
drawingState = .ended
drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
default: break
}
}
private func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContext(drawingBoardImageView.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingBoardImageView.frame.size.width, height: drawingBoardImageView.frame.size.height))
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(brush.lineCap)
context?.setAlpha(brush.opacity)
context?.setLineWidth(brush.width)
context?.setStrokeColor(brush.color.cgColor)
if shouldErase {
context?.setBlendMode(.clear)
} else {
context?.setBlendMode(.normal)
}
context?.strokePath()
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return }
if drawingState == .ended {
drawingUndoManager.registerForUndo(image)
}
tempImageView.image = image
tempImageView.alpha = 1.0
UIGraphicsEndImageContext()
}
Related Topics
Invalid Signature - Code Object Is Not Signed at All
Randomly Choosing an Item from a Swift Array Without Repeating
MySQL and Swift - Upload Image and File || Would It Be Better to Use Alamofire
How to Count Rows in a Parse Class Programmatically in an iOS App Using Swift
How to Make a Call Which Begin with * in Iphone
Cast Value of Type 'Uitableviewcell' to Custom Cell
Repeat Interval for Unnotification
What Privacy-Violating or Device-Changing Things How to Do on an Iphone
Sending Data from One Viewcontroller to Another
How to Make a Swiftui Gesture That Keeps Running Code While the View Is Pressed
How to Force My Keyboard to Be Up on My Program's Start in Swift
How to Use Device Instead of Simulator
How to Properly Change My Status Bar Style in Swift 2/ iOS 9
Detect Shake Gesture iOS Swift
How to Link Googleanalytics 3.01 with Xcode 5 (Missing Required Architecture X86_64)