2013-04-25 41 views
1

我使用触摸事件绘制了签名我从论坛上获得了代码,它的工作正常,但问题是它没有画出正确的线条,就好像我们将线条画成线条一样在该行崩溃阿里这里是我的代码使用触摸在iphone应用程序中绘制线条事件

这里是我绘画的屏幕

enter image description here

- (void)viewDidLoad { 
    [super viewDidLoad]; 
drawImage = [[UIImageView alloc] initWithImage:nil]; 
drawImage.frame = self.view.frame; 
[self.view addSubview:drawImage]; 
self.view.backgroundColor = [UIColor lightGrayColor]; 
mouseMoved = 0; 
    } 

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

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

if ([touch tapCount] == 2) { 
    drawImage.image = nil; 
    return; 
} 

lastPoint = [touch locationInView:self.view]; 
lastPoint.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); 
[drawImage.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(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 

mouseMoved++; 

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

    } 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
    drawImage.image = nil; 
    return; 
    } 


    if(!mouseSwiped) { 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.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(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    CGContextFlush(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
     } 
      } 
+1

你可以改述你的问题和问题吗? – alecail 2013-06-06 20:27:18

回答

0

您可以添加按钮,按下该按钮的箭头是添加屏幕,你可以旋转和使用触摸调整大小。

+0

我希望像普通人一样使用此代码进行签名 – 2013-04-25 07:43:22

相关问题