From b62ff782ae6fa6243fa5cabef9c864c4c40175d6 Mon Sep 17 00:00:00 2001 From: Justin Anderson Date: Tue, 25 Mar 2014 17:18:53 -0600 Subject: [PATCH] Added a Save function --- Smooth Line View.xcodeproj/project.pbxproj | 34 ++-- Smooth Line View/SmoothLineView.h | 5 +- Smooth Line View/SmoothLineView.m | 75 +++++-- .../Smooth_Line_ViewViewController.h | 4 +- .../Smooth_Line_ViewViewController.m | 34 +++- .../Smooth_Line_ViewViewController.xib | 188 ++++++------------ 6 files changed, 167 insertions(+), 173 deletions(-) mode change 100644 => 100755 Smooth Line View/SmoothLineView.h mode change 100644 => 100755 Smooth Line View/SmoothLineView.m mode change 100644 => 100755 Smooth Line View/Smooth_Line_ViewViewController.h mode change 100644 => 100755 Smooth Line View/Smooth_Line_ViewViewController.m mode change 100644 => 100755 Smooth Line View/Smooth_Line_ViewViewController.xib diff --git a/Smooth Line View.xcodeproj/project.pbxproj b/Smooth Line View.xcodeproj/project.pbxproj index 796373c..78bea22 100644 --- a/Smooth Line View.xcodeproj/project.pbxproj +++ b/Smooth Line View.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 = ""; }; - 6B250CCB13F9ED20002A5D9D /* SmoothLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SmoothLineView.h; sourceTree = ""; }; - 6B250CCC13F9ED20002A5D9D /* SmoothLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SmoothLineView.m; sourceTree = ""; }; 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; }; @@ -37,9 +35,11 @@ 6B93BE7D13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Smooth_Line_ViewAppDelegate.h; sourceTree = ""; }; 6B93BE7E13F3374200DFAB23 /* Smooth_Line_ViewAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Smooth_Line_ViewAppDelegate.m; sourceTree = ""; }; 6B93BE8113F3374200DFAB23 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainWindow.xib; sourceTree = ""; }; - 6B93BE8313F3374200DFAB23 /* Smooth_Line_ViewViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Smooth_Line_ViewViewController.h; sourceTree = ""; }; - 6B93BE8413F3374200DFAB23 /* Smooth_Line_ViewViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Smooth_Line_ViewViewController.m; sourceTree = ""; }; - 6BA2EACB15E7F21C009779C5 /* Smooth_Line_ViewViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Smooth_Line_ViewViewController.xib; sourceTree = ""; }; + D0D210C518E24618003422D9 /* Smooth_Line_ViewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Smooth_Line_ViewViewController.h; sourceTree = ""; }; + D0D210C618E24618003422D9 /* Smooth_Line_ViewViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Smooth_Line_ViewViewController.m; sourceTree = ""; }; + D0D210C718E24618003422D9 /* Smooth_Line_ViewViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Smooth_Line_ViewViewController.xib; sourceTree = ""; }; + D0D210C818E24618003422D9 /* SmoothLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SmoothLineView.h; sourceTree = ""; }; + D0D210C918E24618003422D9 /* SmoothLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SmoothLineView.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -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"; @@ -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; }; @@ -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; }; @@ -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; }; diff --git a/Smooth Line View/SmoothLineView.h b/Smooth Line View/SmoothLineView.h old mode 100644 new mode 100755 index 43847d0..e8b7511 --- a/Smooth Line View/SmoothLineView.h +++ b/Smooth Line View/SmoothLineView.h @@ -26,14 +26,13 @@ #import - - @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 diff --git a/Smooth Line View/SmoothLineView.m b/Smooth Line View/SmoothLineView.m old mode 100644 new mode 100755 index 3026661..10b50f4 --- a/Smooth Line View/SmoothLineView.m +++ b/Smooth Line View/SmoothLineView.m @@ -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; @@ -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); @@ -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; @@ -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); @@ -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 diff --git a/Smooth Line View/Smooth_Line_ViewViewController.h b/Smooth Line View/Smooth_Line_ViewViewController.h old mode 100644 new mode 100755 index 7334612..36fb887 --- a/Smooth Line View/Smooth_Line_ViewViewController.h +++ b/Smooth Line View/Smooth_Line_ViewViewController.h @@ -28,4 +28,6 @@ #import #import "SmoothLineView.h" -@interface Smooth_Line_ViewViewController : UIViewController @end +@interface Smooth_Line_ViewViewController : UIViewController + +@end diff --git a/Smooth Line View/Smooth_Line_ViewViewController.m b/Smooth Line View/Smooth_Line_ViewViewController.m old mode 100644 new mode 100755 index 9c8b625..0afd12f --- a/Smooth Line View/Smooth_Line_ViewViewController.m +++ b/Smooth Line View/Smooth_Line_ViewViewController.m @@ -30,6 +30,7 @@ #import @interface Smooth_Line_ViewViewController () +@property (strong, nonatomic) IBOutlet UIView *canvasView; @property (nonatomic) SmoothLineView * canvas; @end @@ -37,19 +38,42 @@ @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 diff --git a/Smooth Line View/Smooth_Line_ViewViewController.xib b/Smooth Line View/Smooth_Line_ViewViewController.xib old mode 100644 new mode 100755 index 09fa611..3f0d017 --- a/Smooth Line View/Smooth_Line_ViewViewController.xib +++ b/Smooth Line View/Smooth_Line_ViewViewController.xib @@ -1,125 +1,63 @@ - - - - 1296 - 12A269 - 2549 - 1187 - 624.00 - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 1498 - - - IBProxyObject - IBUIView - - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - PluginDependencyRecalculationVersion - - - - - IBFilesOwner - IBIPadFramework - - - IBFirstResponder - IBIPadFramework - - - - 274 - {{0, 20}, {768, 1004}} - - - - - 3 - MQA - - 2 - - - - 2 - - IBIPadFramework - - - - - - - view - - - - 3 - - - - - - 0 - - - - - - 1 - - - - - -1 - - - File's Owner - - - -2 - - - - - - - Smooth_Line_ViewViewController - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - UIResponder - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - - - - 3 - - - - - Smooth_Line_ViewViewController - UIViewController - - IBProjectSource - ./Classes/Smooth_Line_ViewViewController.h - - - - - 0 - IBIPadFramework - - com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS - - - YES - 3 - 1498 - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +