draw line between two points in iphone?

后端 未结 3 801
猫巷女王i
猫巷女王i 2020-12-11 09:41

\"enter The above picture indicate the redpoint, i like to join those points.

I like t

相关标签:
3条回答
  • 2020-12-11 10:33

    Create your UIImageView subclass, add some methods which will allow to add point's on user touch (will draw them in drawRect: and also place into C-array) and one method to "commit" line. Commit line will simply go through C-array and draw them in context with CG methods.

    0 讨论(0)
  • 2020-12-11 10:39

    You should never draw outside of drawRect:. After detecting a touch, you need to store that information in an ivar and invoke [self setNeedsDisplay] which will call drawRect: at the proper time.

    Also, if you are targeting iOS 3.2+ you should consider using gesture recognizers.

    0 讨论(0)
  • 2020-12-11 10:42

    Use the following code It will work:-

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        mouseSwiped = NO;
        UITouch *touch = [touches anyObject];
    
        if ([touch tapCount] == 2) {
            [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"white" ofType:@"png"]]];
            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.drawImage];
        currentPoint.y -= 20;
    
        NSLog(@"current Point is x: %d, y: %d",currentPoint.x,currentPoint.y);
    
        UIGraphicsBeginImageContext(self.drawImage.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        lastPoint = currentPoint;
    
    }
    

    Feel Free to ask..

    0 讨论(0)
提交回复
热议问题