drawing with finger on UIImageView with a scroll View
I need to make an UIImageView, on a scroll View, active for touching. is a little UiimageView 100x50 were people can make the signature.
I am using the same code and it works, but with scroll view it doesn't. I looked around, but I could not find a code for that.
.h
UIImageView *drawImage;
@property (nonatomic, retain) IBOutlet UIImageView *drawImage;
.m
@synthesize drawImage;
- (void)viewDidLoad {
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320,1540)];
if ([MFMailComposeViewController canSendMail])
button.enabled = YES;
drawImage.userInteractionEnabled = YES;
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchesBegan:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[drawImage addGestureRecognizer:gestureRec];
[gestureRec release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(printItem) forControlEvents:UIControlEventTouchDown];
btn.frame = CGRectMake(75, 0, 44, 44);
[btn setImage:[UIImage imageNamed:@"print.png"] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 3) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:drawImage];
// lastPoint.y -=20;// only if signature goes bottom of the screen
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:drawImage];
currentPoint.y -=5; // only if signature goes bottom of the screen
UIGraphicsBeginImageContext(drawImage.frame.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(currentContext, kCGLineCapRound);
CGContextSetLineWidth(currentContext, 2.0);
CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(currentContext, currentPoint.x, currentPoint.y);
CGContextStrokePath(currentContext);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 3) {
drawImage.im开发者_JAVA百科age = nil;
return;
}
if(!mouseSwiped) {
UIGraphicsBeginImageContext(drawImage.frame.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(currentContext, kCGLineCapRound);
CGContextSetLineWidth(currentContext, 3.0);
CGContextSetRGBStrokeColor(currentContext, 1.0, 1.0, 2.0, 2.0);
CGContextMoveToPoint(currentContext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(currentContext, lastPoint.x, lastPoint.y);
CGContextStrokePath(currentContext);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
I tried the NSZombieEnabled and I get a SIGABRIT on the UITouch line on touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 3) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:drawImage];
// lastPoint.y -=20;// only if signature goes bottom of the screen
}
and a SIGABRIT here, in the int main(int argc, char *argv[]) line
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
I still get the error like before:
2011-05-10 22:14:22.509 anamnesiprova[90427:207] -[UITapGestureRecognizer anyObject]: unrecognized selector sent to instance 0x6bd5ba0
2011-05-10 22:14:22.512 anamnesiprova[90427:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer anyObject]: unrecognized selector sent to instance 0x6bd5ba0'
I think you have to set userInteractionEnabled
of your UIImageView to YES.
Another idea would be to add a GestureRecognizer to your imageView, for the beginning a simple one:
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethodToBeCalled)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myImageView addGestureRecognizer:gestureRec];
[gestureRec release];
精彩评论