2

맨 위에 작은 캐럿 화살표가있는 둥근 사각형을 그리는 다음 코드가 있습니다. 나는 위와 왼쪽, 오른쪽, 또는 아래쪽에 내가 지정한 것에 따라 화살표를 그리는 메소드를 생성 할 수 있기를 원한다. (아마도 enum 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

시작 지점부터 알려 드릴 수 있습니다. 의 중심에

- (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 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 : 간단한 사각형 그리기

: 첫째, 나는 사각형과 화살표를 분리하는 것 직사각형

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

다른 지점의 위치 (210). 이 샘플을 가지고 더 나아갈 수 있기를 바랍니다.

관련 문제