I am working on a bluetooth drawing app. So far I can pick the color and size of the pencil I want as well as load a background image and draw a bezier curved line on the sending device in my app and have the same time show up on the receiving device. My only problem lies in that when I starting to draw a line on the sending device. If I pause when I first start drawing the line then start to move my finger the log shows "touchesBegan" and on the receiving device the line is drawn fine. However, if when I start to draw my finger is moving, on the sending device NO "touchesBegan" is called and although there is no line between the previously draw line and the current line on the sending device there is a line between the last line and the current line.
Here is my code:
//this is making the drawing on the sending device.
//this is sending the info from device to device.
//this is receiving the info on the 2nd device and interpreting it.
// and this is how I draw the lines on the receiving device.
If anyone can help me I would really appreciate it. I'm not sure how the sending device knows not to draw the in between line when "touchesBegan" is not called.
but I would like to be able to make this same thing happen on the receiving device. Thanks
Here is my code:
//this is making the drawing on the sending device.
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchBegna");
touchString = @"touch Began";
pencilString = king;
NSLog(@"pencilStringbegin:%@",pencilString);
self.RealImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);
self.imageView.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);
self.zoom = ax2*subtract;
NSLog(@"zoom:%f",zoom);
self.first= ay2*subtract;
NSLog(@"first:%f",first);
ctr = 0;
UITouch *touch = [touches anyObject];
pts[0] = [touch locationInView:self.tempImage];
lastPoint = [touch locationInView:tempImage];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
touchString = @"touch Moved";
NSLog(@"touchMoved");
self.textview4.text =pencilString;
zoomStringOut = @"0";
NSLog(@"pencilString:%@",pencilString);
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.tempImage];
currentPoint = [touch locationInView:tempImage];
ctr++;
pts[ctr] = p;
if (ctr == 4)
{
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
[self draw2];
// replace points and get ready to handle the next segment
pts[0] = pts[3];
pts[1] = pts[4];
ctr = 1;
}
NSLog(@"ctr:%d",ctr);
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
touchString = @"touch Ended";
NSLog(@"touchEnded");
king = pencilString;
pencilString = @"clear";
NSLog(@"touchstopped");
textview4.text = pencilString;
zoomStringOut = @"0";
[path removeAllPoints];
ctr = 0;
UIGraphicsBeginImageContext(self.tempImage.frame.size);
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
[self.imageView.image drawInRect:CGRectMake(0,0, self.imageView.frame.size.width, self.imageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempImage.image = nil;
UIGraphicsEndImageContext();
}
- (void)draw2
{
if ([pencilString isEqualToString:@"clear2"]) {
UIGraphicsBeginImageContext(imageView.frame.size);
[[UIColor clearColor] setStroke];
[imageView.image drawInRect:CGRectMake(0,0,imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[path setLineWidth:20.0];
[path stroke];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}
else {
UIGraphicsBeginImageContext(self.tempImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
if ([pencilString isEqualToString:@"red"]) {
[[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
[path setLineWidth:w*see];
}
[path stroke];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
// CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
}
}
//this is sending the info from device to device.
Code:
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
{
NSLog(@"Central subscribed to characteristic");
ax = lastPoint.x;
ay = lastPoint.y;
messageString = self.message.text;
NSLog(@"ax:%d",ax);
NSLog(@"ay:%d",ay);
xString = [NSString stringWithFormat:@"%f",ax/see];
textView.text = xString;
yString = [NSString stringWithFormat:@"%f",ay/see];
textView3.text = yString;
zoomStringOut =[NSString stringWithFormat:@"%f",see];
xString2 =[NSString stringWithFormat:@"%d",ax2];
yString2 = [NSString stringWithFormat:@"%d",ay2];
widthString = [NSString stringWithFormat:@"%d",w];
NSLog(@"datatosend2:%@",dataToSend );
// Get the data
NSString *stringZero = touchString;
NSString *stringZeroa = [stringZero stringByAppendingString:@"ᴃ"];
NSString *stringOne = [stringZeroa stringByAppendingString:self.textView.text];
NSString *stringTwo = [stringOne stringByAppendingString:@"ᴃ"];
NSString *stringThree = [stringTwo stringByAppendingString:self.textView3.text];
NSString *stringFour = [stringThree stringByAppendingString:@"ᴃ"];
NSString *stringFive = [stringFour stringByAppendingString:self.textview4.text];
NSString *stringSix = [stringFive stringByAppendingString:@"ᴃ"];
NSString *stringSeven = [stringSix stringByAppendingString:messageString];
NSString *stringEight = [stringSeven stringByAppendingString:@"ᴃ"];
NSString *stringNine = [stringEight stringByAppendingString:zoomStringOut];
NSString *stringTen = [stringNine stringByAppendingString:@"ᴃ"];
NSString *stringEleven = [stringTen stringByAppendingString:xString2];
NSString *stringTwelve = [stringEleven stringByAppendingString:@"ᴃ"];
NSString *stringThirteen = [stringTwelve stringByAppendingString:yString2];
NSString *stringFourTeen = [stringThirteen stringByAppendingString:@"ᴃ"];
NSString *stringFifteen = [stringFourTeen stringByAppendingString:widthString];
self.dataToSend = [stringFifteen dataUsingEncoding:NSUTF8StringEncoding];
[self.peripheralManager updateValue:dataToSend forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];
NSLog(@"stringOne:%@",stringOne );
NSLog(@"stringThirteen:%@",stringFifteen );
// Reset the index
self.sendDataIndex = 0;
// Start sending
[self sendData];
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic
{
NSLog(@"Central unsubscribed from characteristic");
}
- (void)sendData
{
[self.peripheralManager updateValue:dataToSend forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];
}
- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral
{[self sendData];
// Start sending again
}
//this is receiving the info on the 2nd device and interpreting it.
Code:
NSArray *coorArray = [stringFromData componentsSeparatedByString:@"ᴃ"];
touchStringIn =[coorArray objectAtIndex:0];
firstString = [coorArray objectAtIndex:1];
secondString = [coorArray objectAtIndex:2];
thirdString = [coorArray objectAtIndex:3];
secondString2 =[coorArray objectAtIndex:4];
zoomStringIn = [coorArray objectAtIndex:5];
firstString2 = [coorArray objectAtIndex:6];
thirdString2 = [coorArray objectAtIndex:7];
widthStringIn = [coorArray objectAtIndex:8];
NSLog(@"firststring:%@",firstString);
NSLog(@"secondString;%@",secondString);
NSLog(@"thirdString;%@",thirdString);
NSLog(@"secondString2;%@",secondString2);
zoom = zoomStringIn.intValue;
NSLog(@"zoom:%f",zoom);
x = firstString.intValue*zoom;
message2.text = secondString2;
y = secondString.intValue*zoom;
if ([thirdString isEqualToString:@"clear"]) {
lastPoint = CGPointMake(x, y);
}
else {}
currentPoint3 = CGPointMake(x, y) ;
x2 = firstString2.intValue;
y2 = thirdString2.intValue;
NSLog(@"firstString2:%d",x2);
zoom = zoomStringIn.intValue;
NSLog(@"zoom:%f",zoom);
// and this is how I draw the lines on the receiving device.
Code:
if ([touchStringIn isEqualToString:@"touch Began"]) {
NSLog(@"touch Began in");
self.pencilString = thirdString;
self.RealImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
self.imageView.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
self.tempImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
/* }
if ([pencilString isEqualToString:@"clear"]) {*/
NSLog(@"pencilString;%@",pencilString);
ctr = 0;
pts[0] = currentPoint3;
lastPoint = currentPoint3;
}
if ([touchStringIn isEqualToString:@"touch Moved"]) {
NSLog(@"touch Moved in");
self.pencilString = thirdString;
NSLog(@"pencilString;%@",pencilString);
ctr++;
pts[ctr] = currentPoint3;
NSLog(@"ctr:%d",ctr);
NSLog(@"pts[ctr].x:%f",pts[ctr].x);
NSLog(@"currentPoint.x:%f",currentPoint3.x);
NSLog(@"currentPoint.y:%f",currentPoint3.y);
self.RealImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
self.imageView.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
self.tempImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
if (ctr == 4)
{
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
[self draw3];
// replace points and get ready to handle the next segment
pts[0] = pts[3];
pts[1] = pts[4];
ctr = 1;
}
NSLog(@"ctr:%d",ctr);
lastPoint = currentPoint3;
}
if ([touchStringIn isEqualToString:@"touch Ended"]) {
NSLog(@"touch Ended in");
pencilString = thirdString;
NSLog(@"pencilString;%@",pencilString);
[path removeAllPoints];
ctr = 0;
NSLog(@"ctr:%d",ctr);
UIGraphicsBeginImageContext(self.tempImage.frame.size);
self.RealImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
self.imageView.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
self.tempImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
[self.imageView.image drawInRect:CGRectMake(0,0, self.imageView.frame.size.width, self.imageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempImage.image = nil;
UIGraphicsEndImageContext();
}
}
- (void) draw3
{
if ([pencilString isEqualToString:@"clear2"]) {
UIGraphicsBeginImageContext(imageView.frame.size);
[[UIColor clearColor] setStroke];
[imageView.image drawInRect:CGRectMake(0,0,imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint3.x, currentPoint3.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[path setLineWidth:20.0];
[path stroke];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}
else{
NSLog(@"this is being called");
UIGraphicsBeginImageContext(self.tempImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
if ([pencilString isEqualToString:@"red"]) {
[[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
[path setLineWidth:w2*zoom];
}
[path stroke];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
// CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
}
}
If anyone can help me I would really appreciate it. I'm not sure how the sending device knows not to draw the in between line when "touchesBegan" is not called.
but I would like to be able to make this same thing happen on the receiving device. Thanks