iOS Draw Rectagle with curved ends

前端 未结 2 675
误落风尘
误落风尘 2021-01-16 17:41

Imagine a long rectangle (maybe with size 200x20). All sides have straight edges. In my iOS app, this is easy for me to draw:

CGContextFillRect(context, CGRe         


        
相关标签:
2条回答
  • 2021-01-16 18:18

    You may find Jeff LaMarche's RoundedRectView class useful, whether to use instances of directly or even to see how he makes them:

    http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html

    0 讨论(0)
  • 2021-01-16 18:20

    Something like this (untested, so beware of bugs!):

    - (void)drawRect:(CGRect)rect {
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
     CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    
     CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect)-30, CGRectGetHeight(rect)-30);
     CGFloat radius = 0.5f;
    
     CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
     CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
    
     CGContextMoveToPoint(context, minx, midy);
     CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
     CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
     CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
     CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
     CGContextClosePath(context);
     CGContextDrawPath(context, kCGPathFillStroke);
    }
    
    0 讨论(0)
提交回复
热议问题