Как нарисовать сплошную линию на экране iphone - PullRequest
0 голосов
/ 18 октября 2011

Я только изучаю основную графику.Итак, я создал новый проект, включив класс UIView.Теперь у меня есть следующий код в моем файле реализации UIView

-(id) initWithCoder:(NSCoder*)sourceCoder
{
    if( ( self = [super initWithCoder:sourceCoder]))
    {
        //place any other initialization  here
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    CGContextRef    context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context,4);
    CGContextSetStrokeColorWithColor(context, myColor.CGColor);


    CGContextMoveToPoint(context,startPoint.x , startPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    CGContextStrokePath(context);

}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touchPoint = [touches anyObject]; 
    startPoint = [touchPoint locationInView:self];
    endPoint = [touchPoint locationInView:self];

    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    endPoint=[touch locationInView:self];
    [self setNeedsDisplay];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    [self setNeedsDisplay];
}

Но он просто создает линию, и если я снова пытаюсь нарисовать, то предыдущая линия теряется.

Но я хочупредыдущая строка не должна быть потеряна, как я могу это сделать?

Ответы [ 2 ]

0 голосов
/ 18 октября 2011

Установить UIImageView в качестве фона, сохранить контекст (строки) там с помощью:

CGContextStrokePath(UIGraphicsGetCurrentContext());
<your imageView>.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
0 голосов
/ 18 октября 2011

Установите свойство clearsContextBeforeDrawing представления в NO.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...