2016-07-19 11 views
-1

Ich brauche Sie hilft, diesen Rahmen auf UIView mit uibezierpath oder ähnlichem zu zeichnen.Grenze auf UIView mit uibezierpath zeichnen

enter image description here

Ich habe diese Lösung gemacht: Nest ein UIView in einem anderen, aber ich denke, dass es eine bessere Lösung ist:

- (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]; 

} 

Dank.

Antwort

4

Sie können es in einem UIView mit zwei Bezier-Pfade zeichnen. Subklassen der UIView und nimmt diese in dem der UIViewdrawRect Methode

-(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]; 
} 

enter image description here