Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Smooth Line View.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
5EF58F54189D8AF40024C782 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 5EF58F53189D8AF40024C782 /* Default-568h@2x.png */; };
6B250CCD13F9ED20002A5D9D /* SmoothLineView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B250CCC13F9ED20002A5D9D /* SmoothLineView.m */; };
6B8244E614E0FF67007F2487 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B250CCF13FA2127002A5D9D /* QuartzCore.framework */; };
6B93BE6F13F3374200DFAB23 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B93BE6E13F3374200DFAB23 /* UIKit.framework */; };
6B93BE7113F3374200DFAB23 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B93BE7013F3374200DFAB23 /* Foundation.framework */; };
Expand All @@ -17,14 +16,13 @@
6B93BE7B13F3374200DFAB23 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B93BE7A13F3374200DFAB23 /* main.m */; };
6B93BE7F13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B93BE7E13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.m */; };
6B93BE8213F3374200DFAB23 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6B93BE8013F3374200DFAB23 /* MainWindow.xib */; };
6B93BE8513F3374200DFAB23 /* Smooth_Line_ViewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B93BE8413F3374200DFAB23 /* Smooth_Line_ViewViewController.m */; };
6BA2EACC15E7F21C009779C5 /* Smooth_Line_ViewViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6BA2EACB15E7F21C009779C5 /* Smooth_Line_ViewViewController.xib */; };
D0D210CA18E24618003422D9 /* Smooth_Line_ViewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D0D210C618E24618003422D9 /* Smooth_Line_ViewViewController.m */; };
D0D210CB18E24618003422D9 /* Smooth_Line_ViewViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D0D210C718E24618003422D9 /* Smooth_Line_ViewViewController.xib */; };
D0D210CC18E24618003422D9 /* SmoothLineView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0D210C918E24618003422D9 /* SmoothLineView.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
5EF58F53189D8AF40024C782 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
6B250CCB13F9ED20002A5D9D /* SmoothLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SmoothLineView.h; sourceTree = "<group>"; };
6B250CCC13F9ED20002A5D9D /* SmoothLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SmoothLineView.m; sourceTree = "<group>"; };
6B250CCF13FA2127002A5D9D /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
6B93BE6A13F3374200DFAB23 /* Smooth Line View.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Smooth Line View.app"; sourceTree = BUILT_PRODUCTS_DIR; };
6B93BE6E13F3374200DFAB23 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
Expand All @@ -37,9 +35,11 @@
6B93BE7D13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Smooth_Line_ViewAppDelegate.h; sourceTree = "<group>"; };
6B93BE7E13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Smooth_Line_ViewAppDelegate.m; sourceTree = "<group>"; };
6B93BE8113F3374200DFAB23 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainWindow.xib; sourceTree = "<group>"; };
6B93BE8313F3374200DFAB23 /* Smooth_Line_ViewViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Smooth_Line_ViewViewController.h; sourceTree = "<group>"; };
6B93BE8413F3374200DFAB23 /* Smooth_Line_ViewViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Smooth_Line_ViewViewController.m; sourceTree = "<group>"; };
6BA2EACB15E7F21C009779C5 /* Smooth_Line_ViewViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Smooth_Line_ViewViewController.xib; sourceTree = "<group>"; };
D0D210C518E24618003422D9 /* Smooth_Line_ViewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Smooth_Line_ViewViewController.h; sourceTree = "<group>"; };
D0D210C618E24618003422D9 /* Smooth_Line_ViewViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Smooth_Line_ViewViewController.m; sourceTree = "<group>"; };
D0D210C718E24618003422D9 /* Smooth_Line_ViewViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Smooth_Line_ViewViewController.xib; sourceTree = "<group>"; };
D0D210C818E24618003422D9 /* SmoothLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SmoothLineView.h; sourceTree = "<group>"; };
D0D210C918E24618003422D9 /* SmoothLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SmoothLineView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -92,11 +92,11 @@
6B93BE7D13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.h */,
6B93BE7E13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.m */,
6B93BE8013F3374200DFAB23 /* MainWindow.xib */,
6B93BE8313F3374200DFAB23 /* Smooth_Line_ViewViewController.h */,
6B93BE8413F3374200DFAB23 /* Smooth_Line_ViewViewController.m */,
6BA2EACB15E7F21C009779C5 /* Smooth_Line_ViewViewController.xib */,
6B250CCB13F9ED20002A5D9D /* SmoothLineView.h */,
6B250CCC13F9ED20002A5D9D /* SmoothLineView.m */,
D0D210C518E24618003422D9 /* Smooth_Line_ViewViewController.h */,
D0D210C618E24618003422D9 /* Smooth_Line_ViewViewController.m */,
D0D210C718E24618003422D9 /* Smooth_Line_ViewViewController.xib */,
D0D210C818E24618003422D9 /* SmoothLineView.h */,
D0D210C918E24618003422D9 /* SmoothLineView.m */,
6B93BE7513F3374200DFAB23 /* Supporting Files */,
);
path = "Smooth Line View";
Expand Down Expand Up @@ -165,9 +165,9 @@
buildActionMask = 2147483647;
files = (
6B93BE7913F3374200DFAB23 /* InfoPlist.strings in Resources */,
D0D210CB18E24618003422D9 /* Smooth_Line_ViewViewController.xib in Resources */,
5EF58F54189D8AF40024C782 /* Default-568h@2x.png in Resources */,
6B93BE8213F3374200DFAB23 /* MainWindow.xib in Resources */,
6BA2EACC15E7F21C009779C5 /* Smooth_Line_ViewViewController.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -178,10 +178,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D0D210CC18E24618003422D9 /* SmoothLineView.m in Sources */,
6B93BE7B13F3374200DFAB23 /* main.m in Sources */,
D0D210CA18E24618003422D9 /* Smooth_Line_ViewViewController.m in Sources */,
6B93BE7F13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.m in Sources */,
6B93BE8513F3374200DFAB23 /* Smooth_Line_ViewViewController.m in Sources */,
6B250CCD13F9ED20002A5D9D /* SmoothLineView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -263,7 +263,7 @@
INFOPLIST_FILE = "Smooth Line View/Smooth Line View-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "C4486A19-69DA-4716-9E18-6EB2EB68880F";
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "";
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
};
Expand Down
5 changes: 2 additions & 3 deletions Smooth Line View/SmoothLineView.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@

#import <UIKit/UIKit.h>



@interface SmoothLineView : UIView

@property (nonatomic, strong) UIColor *lineColor;
@property (nonatomic, assign) CGFloat lineWidth;
@property (nonatomic, assign) BOOL empty;

-(void)loadPath;
-(void)savePath;
-(void)clear;

@end
75 changes: 53 additions & 22 deletions Smooth Line View/SmoothLineView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#define DEFAULT_COLOR [UIColor blackColor]
#define DEFAULT_WIDTH 5.0f
#define DEFAULT_BACKGROUND_COLOR [UIColor whiteColor]
#define DEFAULT_BACKGROUND_COLOR [UIColor clearColor]

static const CGFloat kPointMinDistance = 5.0f;
static const CGFloat kPointMinDistanceSquared = kPointMinDistance * kPointMinDistance;
Expand All @@ -38,6 +38,7 @@ @interface SmoothLineView ()
@property (nonatomic,assign) CGPoint currentPoint;
@property (nonatomic,assign) CGPoint previousPoint;
@property (nonatomic,assign) CGPoint previousPreviousPoint;
@property (nonatomic, retain) NSMutableArray* points;

#pragma mark Private Helper function
CGPoint midPoint(CGPoint p1, CGPoint p2);
Expand All @@ -49,25 +50,49 @@ @implementation SmoothLineView {
}

#pragma mark UIView lifecycle methods

- (void) encodeWithCoder:(NSCoder *)aCoder {
UIBezierPath *test = [[UIBezierPath alloc] init];
test.CGPath = _path;
[aCoder encodeObject:test forKey:@"path"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
self = [super init];

if (self) {
// NOTE: do not change the backgroundColor here, so it can be set in IB.
_path = CGPathCreateMutable();
_lineWidth = DEFAULT_WIDTH;
_lineColor = DEFAULT_COLOR;
_empty = YES;
_path = CGPathCreateMutable();
_lineWidth = 5.0f;
_lineColor = [UIColor blackColor];
}

return self;
}
-(void)savePath {
UIBezierPath *savePath = [[UIBezierPath alloc] init];
savePath.CGPath = _path;
NSMutableData *saveData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:saveData];
[archiver encodeObject:savePath forKey:@"path"];
[archiver finishEncoding];

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"path.p"];
[saveData writeToFile:path atomically:YES];
}
-(void)loadPath {
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"path.p"];
NSData* bytesData = [NSData dataWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:bytesData];
UIBezierPath *savePath = [unarchiver decodeObjectForKey:@"path"];
CGPathAddPath(_path, NULL, savePath.CGPath);
[self setNeedsDisplay];
}

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {

self.points = [[NSMutableArray alloc] init];
self.backgroundColor = DEFAULT_BACKGROUND_COLOR;
_path = CGPathCreateMutable();
_lineWidth = DEFAULT_WIDTH;
Expand All @@ -85,7 +110,7 @@ - (void)drawRect:(CGRect)rect {

// get the graphics context and draw the path
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, _path);
CGContextAddPath(context, _path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
Expand Down Expand Up @@ -143,31 +168,37 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

// to represent the finger movement, create a new path segment,
// a quadratic bezier path from mid1 to mid2, using previous as a control point
CGMutablePathRef subpath = CGPathCreateMutable();
CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(subpath, NULL,
self.previousPoint.x, self.previousPoint.y,
mid2.x, mid2.y);

// compute the rect containing the new segment plus padding for drawn line
CGRect bounds = CGPathGetBoundingBox(subpath);
CGRect drawBox = CGRectInset(bounds, -2.0 * self.lineWidth, -2.0 * self.lineWidth);

// append the quad curve to the accumulated path so far.
[self setNeedsDisplayInRect:[self addPointToLine:mid1 mid2:mid2]];
}
-(CGRect) addPointToLine:(CGPoint)mid1 mid2:(CGPoint)mid2 {
CGMutablePathRef subpath = CGPathCreateMutable();
CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(subpath, NULL,
self.previousPoint.x, self.previousPoint.y,
mid2.x, mid2.y);

// compute the rect containing the new segment plus padding for drawn line
CGRect bounds = CGPathGetBoundingBox(subpath);
CGRect drawBox = CGRectInset(bounds, -2.0 * self.lineWidth, -2.0 * self.lineWidth);

// append the quad curve to the accumulated path so far.
CGPathAddPath(_path, NULL, subpath);
CGPathRelease(subpath);

[self setNeedsDisplayInRect:drawBox];
return drawBox;
}

#pragma mark interface

-(void)clear {
CGMutablePathRef oldPath = _path;
CGMutablePathRef oldPath = _path;
CFRelease(oldPath);
_path = CGPathCreateMutable();
[self setNeedsDisplay];


}



@end

4 changes: 3 additions & 1 deletion Smooth Line View/Smooth_Line_ViewViewController.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@
#import <UIKit/UIKit.h>
#import "SmoothLineView.h"

@interface Smooth_Line_ViewViewController : UIViewController @end
@interface Smooth_Line_ViewViewController : UIViewController

@end
34 changes: 29 additions & 5 deletions Smooth Line View/Smooth_Line_ViewViewController.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,50 @@
#import <CoreMotion/CoreMotion.h>

@interface Smooth_Line_ViewViewController ()
@property (strong, nonatomic) IBOutlet UIView *canvasView;
@property (nonatomic) SmoothLineView * canvas;
@end

@implementation Smooth_Line_ViewViewController

- (void)viewDidLoad
{
SmoothLineView * smoothLineView =[[SmoothLineView alloc] initWithFrame:self.view.bounds ];
self.canvas = smoothLineView;
[self.view addSubview:smoothLineView];

SmoothLineView * smoothLineView = [[SmoothLineView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
self.canvas = smoothLineView;
[self.canvasView addSubview:smoothLineView];

}

-(BOOL)canBecomeFirstResponder {
return YES;
return YES;
}
- (IBAction)buttonDown:(id)sender {

[self.canvas clear];
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
[self.canvas clear];
[self.canvas clear];
}

- (IBAction)savePressed:(id)sender {

[self.canvas savePath];


}

- (IBAction)loadPressed:(id)sender {

[self.canvas loadPath];

}


@end


Loading