2

У меня есть следующий код, который рисует закругленный прямоугольник с небольшой стрелкой в ​​верхней части. Я хочу, чтобы иметь возможность создать метод, который либо рисует стрелку вверху, влево, вправо или снизу в зависимости от того, что я укажу (я, скорее всего, перейду в перечисление typedef).Как изменить направление стрелки этого UIBezierPath

Вот что мой код создает прямо сейчас:

enter image description here

Может кто-нибудь помочь мне сделать этот код более динамичным/гибким, так что я могу это сделать?

 CGContextBeginPath(context); 
     CGContextMoveToPoint(context, kBubbleBorderRadius + 0.5f, kCaretHeight + 0.5f); 
     CGContextAddLineToPoint(context, round(currentFrame.size.width/2.0f - (kCaretHeight * 2.0)/2.0f) + 0.5f, kCaretHeight + 0.5f); 
     CGContextAddLineToPoint(context, round(currentFrame.size.width/2.0f) + 0.5f, 0.5f); 
     CGContextAddLineToPoint(context, round(currentFrame.size.width/2.0f + (kCaretHeight * 2.0)/2.0f) + 0.5f, kCaretHeight + 0.5f); 
     CGContextAddArcToPoint(context, currentFrame.size.width - 0.5f, kCaretHeight + 0.5f, currentFrame.size.width- 0.5f, currentFrame.size.height - 0.5f, kBubbleBorderRadius); 
     CGContextAddArcToPoint(context, currentFrame.size.width - 0.5f, currentFrame.size.height - 0.5f, round(currentFrame.size.width/2.0f + (kCaretHeight * 2.0)/2.0f) + 0.5f, currentFrame.size.height - 0.5f, kBubbleBorderRadius); 
     CGContextAddArcToPoint(context, 0.5f, currentFrame.size.height - 0.5f, 0.5f, kCaretHeight + 0.5f, kBubbleBorderRadius); 
     CGContextAddArcToPoint(context, 0.5f, kCaretHeight + 0.5f, currentFrame.size.width - 0.5f, kCaretHeight + 0.5f, kBubbleBorderRadius); 
     CGContextClosePath(context); 
     CGContextDrawPath(context, kCGPathFill); 
+0

Не могли бы вы предоставить желаемое графическое представление ... – Injectios

+0

@Injectios добавили изображение текущего генерации кода на вопрос, спасибо. –

+0

Это проще, если вы нарисуете треугольник отдельно от тела. – thelaws

ответ

1

Я могу дать вам указание начать с. Во-первых, я хотел бы разделить прямоугольник и стрелы:

рисунок простой прямоугольник:

const CGFloat kRectangleOffset = 5; // This is offset to have some space for arrow 

// Draw simple rectangle in DrawRect method 
    CGRect frame = CGRectMake(0+kRectangleOffset, 
           0+kRectangleOffset, 
           rect.size.width-kRectangleOffset*2, 
           rect.size.height-kRectangleOffset*2); 

    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: frame]; 
    [UIColor.grayColor setFill]; 
    [rectanglePath fill]; 

Добавить в подвид: Fe

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    BoxView *box = [[BoxView alloc] initWithFrame:CGRectMake(100, 100, 200, 80)]; 
    box.backgroundColor = [UIColor redColor]; // I specially set red color to see it's full surface... 
    [self.view addSubview:box]; 
} 

enter image description here

Тогда простой стрелкой рисунок в центре прямоугольник

const CGFloat kArrowWidth = 10; 

    // Draw Bezier arrow on top consists of 3 points 
    UIBezierPath* bezierPathTop = UIBezierPath.bezierPath; 
    [bezierPathTop moveToPoint: CGPointMake(rect.size.width/2-kArrowWidth/2, kRectangleOffset)]; 
    [bezierPathTop addLineToPoint: CGPointMake(rect.size.width/2+kArrowWidth/2, kRectangleOffset)]; 
    [bezierPathTop addLineToPoint: CGPointMake(rect.size.width/2, 0)]; 
    [UIColor.blueColor setFill]; 
    [bezierPathTop fill]; 

enter image description here

И, наконец, та же стрелка, но с разных точек расположения

UIBezierPath* bezierPathLeft = UIBezierPath.bezierPath; 
[bezierPathLeft moveToPoint: CGPointMake(kRectangleOffset, rect.size.height/2-kArrowWidth/2)]; 
[bezierPathLeft addLineToPoint: CGPointMake(kRectangleOffset, rect.size.height/2+kArrowWidth/2)]; 
[bezierPathLeft addLineToPoint: CGPointMake(0, rect.size.height/2)]; 
[UIColor.blueColor setFill]; 
[bezierPathLeft fill]; 

enter image description here

Вам нужно сделать только один стрелку точно на основе, скажем, направление стрелки ENUM. Я надеюсь, что вы можете пойти дальше с этим образцом

Смежные вопросы