I am trying to build a lock screen app.
I have managed to get the path to draw from one point and update when drawing the line from one point to another. I am now trying to build a "snap to" function where the line "snaps to" the dot and then that dot's mid point will be the next "point from" to use for the next line.
The issue is that the line will only draw horizontally when using the snap to function, when I try to draw it vertically, the line disappears and does not draw between the last dot and the vertical or diagonal dot.
what I mean is this:
when I try to draw from the bottom right to the top left:
This is my code:
TOUCHES BEGAN:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
process_touch(touches)
drawPath = UIBezierPath()
drawPath.moveToPoint((touches.first?.locationInView(self.superview))!)
shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.blackColor().CGColor
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.lineWidth = 4.0
self.superview!.layer.addSublayer(shapeLayer)
last_x = (touches.first?.locationInView(self.superview).x)!
last_y = (touches.first?.locationInView(self.superview).y)!
}
TOUCHES MOVED (This also draws the line before the snap):
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent!) {
process_touch(touches)
if(shapeLayer != nil) {
shapeLayer.removeFromSuperlayer()
shapeLayer = nil
drawPath.removeAllPoints()
drawPath = nil
}
if(drawPath == nil) {
drawPath = UIBezierPath()
drawPath.moveToPoint((touches.first?.locationInView(self.superview))!)
shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.blackColor().CGColor
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.lineWidth = 8.0
self.superview!.layer.addSublayer(shapeLayer)
}
drawPath.moveToPoint(CGPoint(x: last_x, y: last_y))
drawPath.addLineToPoint((touches.first?.locationInView(self.superview))!)
shapeLayer.path = drawPath.CGPath
setNeedsDisplay()
}
TOUCHES ENDED:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
Globals.alreadySnapped = []
Globals.snappedPoints = []
last_x = (touches.first?.locationInView(self.superview).x)!
last_y = (touches.first?.locationInView(self.superview).y)!
if(shapeLayer != nil) {
shapeLayer.removeFromSuperlayer()
shapeLayer = nil
}
if(drawPath != nil) {
drawPath.removeAllPoints()
drawPath = nil
}
if(main_shapeLayer != nil) {
main_shapeLayer.removeFromSuperlayer()
main_shapeLayer = nil
}
if(main_drawPath != nil) {
main_drawPath.removeAllPoints()
main_drawPath = nil
}
}
DRAWING THE LINE (after the snap):
func draw_line_to(last_location:CGPoint, new_location: CGPoint) {
if(main_drawPath == nil) {
main_drawPath = UIBezierPath()
main_drawPath.moveToPoint(CGPoint(x: last_location.x, y: last_location.y))
main_shapeLayer = CAShapeLayer()
main_shapeLayer.strokeColor = UIColor.blackColor().CGColor
main_shapeLayer.fillColor = UIColor.clearColor().CGColor
main_shapeLayer.lineWidth = 8.0
self.superview!.layer.addSublayer(main_shapeLayer)
}
main_drawPath.moveToPoint(CGPoint(x: last_location.x, y: last_location.y))
main_drawPath.addLineToPoint(CGPoint(x: new_location.x, y: last_location.y))
main_shapeLayer.path = main_drawPath.CGPath
last_x = new_location.x
last_y = new_location.y
setNeedsDisplay()
}
SNAP TO:
func append_point(obstacle_view: HoverButtonView, touch_location: CGPoint) {
Globals.snappedPoints.append(touch_location)
if(main_shapeLayer != nil) {
main_shapeLayer.removeFromSuperlayer()
main_shapeLayer = nil
}
if(main_drawPath != nil) {
main_drawPath.removeAllPoints()
main_drawPath = nil
}
var count = 0
var last_point: CGPoint = CGPoint(x: 0.0, y: 0.0)
for point in Globals.snappedPoints {
print("point (point)")
if(count > 0) {
draw_line_to(last_point, new_location: point)
} else {
draw_line_to(point, new_location: point)
}
last_point = point
count+=1
}
}
Deal with the touch
func process_touch(touches: NSSet!) {
// Get the first touch and its location in this view controller's view coordinate system
let touch = touches.allObjects[0] as! UITouch
let touchLocation = touch.locationInView(self.superview)
obstacleViews = Globals.obstacleViews
for obstacleView in obstacleViews {
// Convert the location of the obstacle view to this view controller's view coordinate system
let obstacleViewFrame = self.superview!.convertRect(obstacleView.frame, fromView: self.superview)
// Check if the touch is inside the obstacle view
if CGRectContainsPoint(obstacleViewFrame, touchLocation) {
if(!Globals.alreadySnapped.contains(obstacleView)) {
Globals.alreadySnapped.append(obstacleView)
var touch_loc = CGPoint(x: obstacleViewFrame.midX, y: obstacleViewFrame.midY)
append_point(obstacleView, touch_location: touch_loc)
}
}
}
}
What changes should I make to the above code to allow horizontal
, vertical
and diagonal
lines to snap to the dots?
Aucun commentaire:
Enregistrer un commentaire