Как отрисовать линию при помощи Core Graphics?

RfLNNvQglsk.jpg
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    
    if (touch.tapCount == 1) {
        lastPoint = [touch locationInView:self.drawImage];
    } else {
        drawImage.image = nil;
    }
    
    FirstPoint=currentPoint;
}





- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    
    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    CGRect drawRect = CGRectMake(0.0f, 0.0f,
                                 self.drawImage.frame.size.width,
                                 self.drawImage.frame.size.height);
    [drawImage.image drawInRect:drawRect];
    
    
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0f);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0f, 0.0f, 0.0f, 1.0f);
    
    
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    
    
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    
    
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0f);
        
    
    
    
    
    
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
  //  lastPoint = currentPoint;
     

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    
    if (UITouchPhaseEnded) {
        
        CGPoint currentPoint = [touch locationInView:self.drawImage];
        
        endPoint=currentPoint;
        
        
        
        
        
    }
    
    
   // [[NSNotificationCenter defaultCenter] postNotificationName:@"metro" object:nil];
    
    
    
}


Рисут "солнышком". Непонятно как реальзовать так, чтобы было как в paint под windows
  • Вопрос задан
  • 2533 просмотра
Решения вопроса 1
@maxonflic Автор вопроса
решено
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 2
AMar4enko
@AMar4enko
А зачем вы закомментировали lastPoint = currentPoint; ? Как раз из-за этого и не работает, похоже.
Ответ написан
ManWithBear
@ManWithBear
Swift Adept, Prague
Сделайте 2 холста. Один для временных изменений (холст А), а другой для уже примененных (холст Б).
Холст А прозрачный и лежит поверх холста Б.
Вашим способом рисуете линию на холсте А, каждый раз при изменении позиции пальца полностью очищаете этот холст и рисуете линию.
Как только касание заканчивается, копируете изменения с холста А на холст Б.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы