Last active
November 6, 2015 11:32
-
-
Save cryophobia/a35e5d5d73d17f5b0a46 to your computer and use it in GitHub Desktop.
Adding Force Touch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void DrawLine (CGPoint pt1, CGPoint pt2, UIColor color, nfloat forcePercentage) | |
{ | |
UIGraphics.BeginImageContext (imgDraw.Frame.Size); | |
using (var g = UIGraphics.GetCurrentContext ()) | |
{ | |
imgDraw.Layer.RenderInContext (UIGraphics.GetCurrentContext ()); | |
var path = new CGPath (); | |
path.AddLines (new CGPoint[] { pt1, pt2 }); | |
//Calc Line width | |
var size = 0.25f * Math.Pow (2, 1.5 * Math.Round (forcePercentage / 10)); | |
if (size > 25) | |
size = 25; | |
g.SetLineWidth ((nfloat)size); | |
color.SetStroke (); | |
Console.WriteLine (forcePercentage); | |
Console.WriteLine (size); | |
g.AddPath (path); | |
g.DrawPath (CGPathDrawingMode.Stroke); | |
imgDraw.Image = UIGraphics.GetImageFromCurrentImageContext (); | |
} | |
UIGraphics.EndImageContext (); | |
} | |
public override void TouchesMoved (NSSet touches, UIEvent evt) | |
{ | |
foreach (UITouch touch in touches) | |
{ | |
UIColor color; | |
colors.TryGetValue (touch.Handle, out color); | |
DrawLine (touch.PreviousLocationInView (imgDraw), touch.LocationInView (imgDraw), color, CalcForcePercentage(touch)); | |
} | |
} | |
nfloat CalcForcePercentage(UITouch touch){ | |
return (touch.Force / touch.MaximumPossibleForce) * 100; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment