创建ios绘图应用程序

时间:2017-11-02 04:39:51

标签: ios objective-c

我试图创建一个简单的绘画应用程序。

- (void)handleSingleTap:(UIPanGestureRecognizer *)recognizer
{
    CGPoint location = [recognizer locationInView:[recognizer.view superview]];
    NSLog(@"Test");

    CGFloat ok = 40;
    self.sheet.test = ok;

    [self.sheet addPoint: location];
    //Do stuff here...
}

此功能绘制线条,但当我抬起手指并更换它时,有一条直线"从最后一点到下一点。我怎么能跳过这个?

2 个答案:

答案 0 :(得分:0)

尝试使用UIGestureRecognizerStateEnded控制它

if(recognizer.state == UIGestureRecognizerStateEnded){

  //End your drawing method.
  isDrawn = NO;
}

根据您的评论,我了解您正在使用变量isDrawn控制您的线条绘制方法。

 if(recognier.state == UIGestureStateBegan){
 [self moveToPoint:point]; // declare the point variable in the header file.
 isDrawn = YES;
}

答案 1 :(得分:0)

希望下面的代码片段有帮助

typedef NS_ENUM(NSInteger, PenType){
    kPenTypeGreen,
    kPenTypeCyan,
    kPenTypeRed,
    kPenTypePurple,
    kPenTypeErase
};

@interface MCStylus() {
    PenType penType;
    CGPoint lastPoint;
    CGPoint secondLastPoint;
    NSInteger currentStylistPageIndex;
    NSMutableArray *arrayOfStylistImages;
}

@property (nonatomic, strong) IBOutlet UIImageView *handwritingView;

@property (nonatomic, strong) UIImageView *drawImage;


- (void)initializeUI {

    arrayOfStylistImages = [[NSMutableArray alloc] init];
    [arrayOfStylistImages addObject:[[UIImage alloc] init]];

    self.drawImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.handwritingView.frame.size.width, self.handwritingView.frame.size.height)];
    self.drawImage.userInteractionEnabled  = true;

    self.handwritingView.backgroundColor = [UIColor whiteColor];
    self.handwritingView.layer.cornerRadius = 5.0f;
    self.handwritingView.layer.borderColor = [UIColor colorWithWhite:230.0f/255.0f alpha:1.0].CGColor;
    self.handwritingView.layer.borderWidth = 2.0f;
    self.handwritingView.clipsToBounds = true;
    [self.handwritingView addSubview:self.drawImage];
}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    NSArray *allTouches = [touches allObjects];
    int count = [allTouches count];
    if(count==1){
        UITouch *touch = [touches anyObject];
        lastPoint = [touch locationInView:self.handwritingView];
        secondLastPoint = lastPoint;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];

    NSArray *allTouches = [touches allObjects];
    int count = [allTouches count];
    if(count==1){
        UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:self.handwritingView];
        UIGraphicsBeginImageContext(self.handwritingView.frame.size);
        if(self.drawImage.image!=NULL)
            [self.drawImage.image drawInRect:CGRectMake(0, 0, self.handwritingView.frame.size.width, self.handwritingView.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
        switch (penType) {
            case kPenTypeGreen:
                CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
                break;
            case kPenTypeCyan:
                CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0);
                break;
            case kPenTypeRed:
                CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
                break;
            case kPenTypePurple:
                CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.5, 0.2, 0.3, 1.0);
                break;
            case kPenTypeErase:
                CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 6.0 * (self.handwritingView.frame.size.height/350));
                CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
                CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),[UIColor clearColor].CGColor);
                break;
            default:
                break;
        }

        CGPoint mid1 = CGPointMake((lastPoint.x+secondLastPoint.x)/2, (lastPoint.y + secondLastPoint.y)/2);
        CGPoint mid2 = CGPointMake((lastPoint.x+currentPoint.x)/2, (lastPoint.y+currentPoint.y)/2);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x, lastPoint.y,mid2.x,mid2.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        secondLastPoint = lastPoint;
        lastPoint = currentPoint;
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];

    if(self.drawImage.image!=NULL)
        [arrayOfStylistImages replaceObjectAtIndex:currentStylistPageIndex withObject:self.drawImage.image];
}