Muju Muju - 1 month ago 16
Objective-C Question

How to Disable and Enable ScrollView on the touch of UIView in Objective c

I'm new in iOS and I'm facing problem for Sign on a view.
enter image description here

enter image description here

I've created a view of Sign.As Shown in Image.

But when I added the view in the ScrollView I'm not able to Sign on it.So my Question is how to disable the scrollview on the view touch. I've used a code same as in this link
How to draw Signature on UIView answer given by user3182143.

Thanks in advance

Answer

I tried with answer what you ask here.

I set scroll inside the view.Then I tried to write on the view but I could not do that.After that I hidden the scroll. Now it works.

SignatureDrawView.h

#import <UIKit/UIKit.h>

@interface SignatureDrawView : UIView

@property (nonatomic, retain) UIGestureRecognizer *theSwipeGesture;
@property (nonatomic, retain) UIImageView *drawImage;
@property (nonatomic, assign) CGPoint lastPoint;
@property (nonatomic, assign) BOOL mouseSwiped;
@property (nonatomic, assign) NSInteger mouseMoved;




- (void)erase;
- (void)setSignature:(NSData *)theLastData;
- (BOOL)isSignatureWrite;

@end

SignatureDrawView.m

#import "SignatureDrawView.h"

@implementation SignatureDrawView

@synthesize theSwipeGesture;
@synthesize drawImage;
@synthesize lastPoint;
@synthesize mouseSwiped;
@synthesize mouseMoved;

#pragma mark - View lifecycle

- (id)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
   if (self) {
    // Initialization code
    }
   return self;
}

- (id)initWithCoder:(NSCoder*)coder 
{
    if ((self = [super initWithCoder:coder]))
    {
      drawImage = [[UIImageView alloc] initWithImage:nil];
      drawImage.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
      [self addSubview:drawImage];
      self.backgroundColor = [UIColor whiteColor];
      mouseMoved = 0;


    }
    return self;
 }

 #pragma mark touch handling

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
     NSArray *array = touch.gestureRecognizers;
     for (UIGestureRecognizer *gesture in array)
     {
        if (gesture.enabled & [gesture isMemberOfClass:[UISwipeGestureRecognizer class]])
        {
            gesture.enabled = NO;
            self.theSwipeGesture = gesture;
        }
      }
   }

   mouseSwiped = NO;
   UITouch *touch = [touches anyObject];

   lastPoint = [touch locationInView:self];

   [[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
   mouseSwiped = YES;

   UITouch *touch = [touches anyObject];
   CGPoint currentPoint = [touch locationInView:self];

   UIGraphicsBeginImageContext(self.frame.size);
   [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
   CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
   CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
   CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
   CGContextBeginPath(UIGraphicsGetCurrentContext());
   CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
   CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
   CGContextStrokePath(UIGraphicsGetCurrentContext());
   drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   lastPoint = currentPoint;

   mouseMoved++;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];


   if (mouseMoved == 10) {
    mouseMoved = 0;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   if(!mouseSwiped)
   {
      UIGraphicsBeginImageContext(self.frame.size);
      [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      CGContextFlush(UIGraphicsGetCurrentContext());
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
     }
    self.theSwipeGesture.enabled = YES;
    mouseSwiped = YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];

 }

#pragma mark Methods

- (void)erase
{
   mouseSwiped = NO;
   drawImage.image = nil;
}

- (void)setSignature:(NSData *)theLastData
{
    UIImage *image = [UIImage imageWithData:theLastData];
    if (image != nil) 
    {
      drawImage.image = [UIImage imageWithData:theLastData];
      mouseSwiped = YES;
    }
 }

 - (BOOL)isSignatureWrite
 {
   return mouseSwiped;
 }

 @end

What I added in above is just I created the post notification for stop the scroll when I touch to signature on view.It is implemented in start,moving and end method.

[[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];

SignatureDrawView.m

Next in ViewController I created the scrollView and UIImageView with UIView.

ViewController.h

#import <UIKit/UIKit.h>
#import "SignatureDrawView.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIScrollView *scroll;

@property (strong, nonatomic) IBOutlet SignatureDrawView *drawSignView;

@property (strong, nonatomic) IBOutlet UITextField *txtFldDesc;

- (IBAction)actionSave:(id)sender;

- (IBAction)actionClear:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize drawSignView,scroll,txtFldDesc;

- (void)viewDidLoad 
{
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
   scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+200);
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScroll:) name:@"stopscroll" object:nil];
}

- (void)stopScroll:(NSNotification *)notification
{
    if([[notification name] isEqualToString:@"stopscroll"])
        scroll.scrollEnabled = NO;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)actionSave:(id)sender 
{
    scroll.scrollEnabled = YES;
   // code for save the signature
    UIGraphicsBeginImageContext(self.drawSignView.bounds.size); 
    [[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
    ....Then do your stuff to save this in DB or server
}
- (IBAction)actionClear:(id)sender 
{
    scroll.scrollEnabled = YES;
    //code for clear the signature
    [self.drawSignView erase];
}
@end

In above viewDidLoad method I added addObserver for stop scrolling.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScroll:) name:@"stopscroll" object:nil];

Finally I implemented the stopScroll: method

Also I set the scroll.scrollEnabled = YES in actionSave and actionclear method

I gave you the solution only after I tried and worked out well.

Check and apply my code.It works fine and perfectly now.

Output Result

enter image description here