-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathCustomDrawing.cs
130 lines (119 loc) · 3.27 KB
/
CustomDrawing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using CoreGraphics;
using UIKit;
namespace SampleBrowser
{
public class LinePath
{
public int LineWidth { get; set; }
public UIColor PenColor { get; set; }
public UIBezierPath Path { get; set; }
}
public class CustomDrawing : UIView
{
List<LinePath> linePathCollection;
UIColor penColor;
// clear the canvas
public void Clear()
{
path.Dispose();
linePathCollection.Clear();
path = new UIBezierPath();
canTouchDraw = false;
SetNeedsDisplay();
}
public CustomDrawing()
{
linePathCollection = new List<LinePath>();
this.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
path = new UIBezierPath();
this.PenColor = UIColor.Black;
this.lineWidth = 8;
this.BackgroundColor = UIColor.White;
}
public event EventHandler<EventArgs> Tapped;
internal virtual void OnTapped(EventArgs args)
{
if (this.Tapped != null)
this.Tapped(this, args);
}
private CGPoint touchLocation;
private CGPoint prevTouchLocation;
private bool canTouchDraw;
private int lineWidth;
UIBezierPath path;
public int LineWidth
{
get { return lineWidth; }
set
{
lineWidth = value;
}
}
public UIColor PenColor
{
get { return penColor; }
set
{
penColor = value;
}
}
public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt)
{
this.OnTapped(new EventArgs());
base.TouchesBegan(touches, evt);
path = new UIBezierPath();
UITouch touch = touches.AnyObject as UITouch;
this.canTouchDraw = true;
this.touchLocation = touch.LocationInView(this);
this.prevTouchLocation = touch.PreviousLocationInView(this);
this.SetNeedsDisplay();
}
public override void TouchesMoved(Foundation.NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
this.touchLocation = touch.LocationInView(this);
this.prevTouchLocation = touch.PreviousLocationInView(this);
this.SetNeedsDisplay();
}
public override void TouchesEnded(Foundation.NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
linePathCollection.Add(new LinePath() { PenColor = penColor, Path = path, LineWidth = lineWidth });
path.ClosePath();
}
public override void Draw(CGRect rect)
{
//base.Draw(rect);
if (this.canTouchDraw)
{
for (int i = 0; i < linePathCollection.Count; i++)
{
LinePath lPath = linePathCollection[i];
lPath.PenColor.SetStroke();
lPath.Path.LineWidth = lPath.LineWidth;
lPath.Path.LineJoinStyle = CGLineJoin.Round;
lPath.Path.LineCapStyle = CGLineCap.Round;
lPath.Path.Stroke();
}
penColor.SetStroke();
path.LineWidth = lineWidth;
path.LineJoinStyle = CGLineJoin.Round;
path.LineCapStyle = CGLineCap.Round;
path.MoveTo(this.prevTouchLocation);
path.AddLineTo(this.touchLocation);
path.Stroke();
}
}
}
}