In this application we will see how to DigitalSignature draw in iPhone. So let see how it will worked.

Step 1: Open the Xcode, Create a new project using Empty Application . Give the application “DigitalSignature”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to create a ViewController class for this application. So select the project New file -> UIViewControllersubclass -> Next -> Give the class name DigitalSignatureView.

Step 4: Open the AppDelegate.h file and make the following changes:

#import <UIKit/UIKit.h>
@class DigitalSignatureView;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
DigitalSignatureView *digitalSignatureView;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) DigitalSignatureView *digitalSignatureView;
@end

Step 5: Open the AppDelegate.m file and make the following changes:

#import "AppDelegate.h"
#import "DigitalSignatureView.h"
@implementation AppDelegate
@synthesize window = _window,digitalSignatureView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
digitalSignatureView = [[DigitalSignatureView alloc]
initWithNibName:@"DigitalSignatureView"
bundle:nil];
[self.window addSubview:digitalSignatureView.view];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end

Step 6: In the DigitalSignatureView.h file, make the following changes:

#import <UIKit/UIKit.h>
@interface DigitalSignatureView : UIViewController
{
CGPoint point;
UIImageView *image;
BOOL mouseSwiped;
int mouseMoved;
}
@property (strong, nonatomic)IBOutlet UIImageView *image;
@end

Step 7: Double click the DigitalSignatureView.xib file and open it to the interface builder. Select the view from interface builder and bring up Attribute Inspector. Change the background color into black. Drag the imageView from the library and
place it to the view. Connect File’s Owner icon to the View and select image. Now save the .xib file , close it and go back to the Xcode.

Step 8: Open the DigitalSignatureView.m file, make the following changes:

#import "DigitalSignatureView.h"
@implementation DigitalSignatureView
@synthesize image;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
#pragma mark – View lifecycle
- (void)viewDidLoad
{
mouseMoved = 0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
image.image = nil;
return;
}
point = [touch locationInView:self.view];
point.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[image.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
point = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
image.image = nil;
return;
}
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[image.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

Step 9: Now Compile and run the application on the Simulator.

Did you enjoy this article?
Share
the
Love
Get Free Updates