Skip to content

Commit

Permalink
fixed start point coordinate issue of #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley-xk committed Jan 28, 2019
1 parent 0b6f517 commit 4a129eb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion MaLiang.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MaLiang'
s.version = '1.1.8'
s.version = '1.1.9'
s.summary = 'MaLiang is a painting Framework based on OpenGL ES.'

s.description = <<-DESC
Expand Down
30 changes: 17 additions & 13 deletions MaLiang/Classes/Canvas.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit

open class Canvas: MLView {

/// specify a brush to paint
open var brush: Brush! {
didSet {
Expand All @@ -28,7 +28,7 @@ open class Canvas: MLView {

// setup gestures
open var paintingGesture: PaintingGestureRecognizer?

open func setupGestureRecognizers() {
/// gesture to render line
paintingGesture = PaintingGestureRecognizer.addToTarget(self, action: #selector(handlePaingtingGesture(_:)))
Expand Down Expand Up @@ -97,7 +97,7 @@ open class Canvas: MLView {
if let doc = document {

super.clear(display: false)

/// find elements to draw, until last clear action
let count = doc.actions.count
var elementsToRedraw: [CanvasElement] = []
Expand Down Expand Up @@ -130,15 +130,15 @@ open class Canvas: MLView {
}
return document?.createTexture(for: element)
}

// MARK: - Bezier
// optimize stroke with bezier path, defaults to true
// private var enableBezierPath = true
// private var enableBezierPath = true
private var bezierGenerator = BezierGenerator()

// MARK: - Drawing Actions
private var lastRenderedPan: Pan?

private func pushPoint(_ point: CGPoint, to bezier: BezierGenerator, force: CGFloat, isEnd: Bool = false) {
let vertices = bezier.pushPoint(point)
if vertices.count >= 2 {
Expand All @@ -149,10 +149,10 @@ open class Canvas: MLView {
let pointStep = brush.pointStep / self.scale
if // end point of line
(isEnd && i == vertices.count - 1) ||
// ignore step
pointStep <= 1 ||
// distance larger than step
(pointStep > 1 && lastPan.point.distance(to: p) >= pointStep)
// ignore step
pointStep <= 1 ||
// distance larger than step
(pointStep > 1 && lastPan.point.distance(to: p) >= pointStep)
{
let f = lastPan.force + deltaForce
let pan = Pan(point: p, force: f)
Expand Down Expand Up @@ -180,7 +180,7 @@ open class Canvas: MLView {
line.color = brush.color.mlcolorWith(opacity: opacity)
self.renderLine(line)
}

// MARK: - Gestures
@objc private func handleTapGesture(_ gesture: UITapGestureRecognizer) {
if gesture.state == .recognized {
Expand All @@ -195,14 +195,18 @@ open class Canvas: MLView {
let location = gesture.gl_location(in: self)

if gesture.state == .began {
/// 取实际的手势起点作为笔迹的起点
let acturalBegin = gesture.acturalBeginLocation
document?.finishCurrentElement()
lastRenderedPan = Pan(point: location, force: gesture.force)
bezierGenerator.begin(with: location)
lastRenderedPan = Pan(point: acturalBegin, force: gesture.force)
bezierGenerator.begin(with: acturalBegin)
}
else if gesture.state == .changed {
pushPoint(location, to: bezierGenerator, force: gesture.force)
}
else if gesture.state == .ended {
else if gesture.state == .ended || gesture.state == .cancelled || gesture.state == .failed {
let count = bezierGenerator.points.count
if count < 3 {
renderTap(at: bezierGenerator.points.first!, to: bezierGenerator.points.last!)
Expand Down
9 changes: 9 additions & 0 deletions MaLiang/Classes/Utils/PaintingGestureRecognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ open class PaintingGestureRecognizer: UIPanGestureRecognizer {
}

// MARK: - Touch Handling
/// due to the delay of pangesture, this is the actural begin point of the gesture
var acturalBeginLocation: CGPoint = CGPoint.zero

override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
/// 修正 pan gesture 的延迟导致的第一个点不正确的问题,
if let first = touches.first {
var location = first.location(in: targetView)
location.y = targetView.bounds.size.height - location.y
acturalBeginLocation = location
}
updateForceFromTouches(touches)
super.touchesBegan(touches, with: event)
}
Expand Down

0 comments on commit 4a129eb

Please sign in to comment.