簡體   English   中英

使用uibezierpath在UIView上繪制邊框

[英]draw border on UIView using uibezierpath

我需要您幫助使用uibezierpath或類似方法在UIView上繪制此邊框。

在此處輸入圖片說明

我已經提出了這個解決方案:在另一個UIView中嵌套一個UIView,但是我認為有更好的解決方案:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.theView.layer.cornerRadius = self.theView.bounds.size.width/2;
    self.theView.layer.masksToBounds = YES;

    UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(4, 4, CGRectGetWidth(self.theView.frame) - 8, CGRectGetHeight(self.theView.frame) - 8)];
    borderView.backgroundColor = [UIColor grayColor];
    borderView.layer.cornerRadius = self.theView.bounds.size.width/2;
    borderView.layer.masksToBounds = YES;

    self.theView.layer.borderWidth = 2;
    self.theView.layer.borderColor = [UIColor redColor].CGColor;
    self.theView.backgroundColor = [UIColor clearColor];
    [self.theView addSubview:borderView];

}

謝謝。

您可以使用兩個貝塞爾曲線路徑在一個UIView繪制它。 子類化UIView並將其包含在UIView drawRect方法中,

-(void)drawRect:(CGRect)frame
{
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 2.5, CGRectGetWidth(frame) - 5, CGRectGetHeight(frame) - 5)];
    [UIColor.redColor setStroke];
    ovalPath.lineWidth = 1;
    [ovalPath stroke];

   UIBezierPath* oval2Path = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame) + 10, CGRectGetMinY(frame) + 10, CGRectGetWidth(frame) - 20, CGRectGetHeight(frame) - 20)];
    [UIColor.lightGrayColor setFill];
    [oval2Path fill];
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM