iOS绘图之画一个渐变的Border

- (void) drawRect:(CGRect)rect
    {
        CGFloat colors [] = {
            1.0, 0.0, 0.0, 1.0,
            1.0, 1.0, 0.0, 1.0,
            0.0, 1.0, 0.0, 1.0,
            0.0, 1.0, 1.0, 1.0,
            0.0, 0.0, 1.0, 1.0,
            1.0, 0.0, 1.0, 1.0
        };

        
        CGRect mapRect = CGRectIntegral(CGRectInset(rect, 100, 250));
        CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
        CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 6);
        CGColorSpaceRelease(baseSpace), baseSpace = NULL;
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetLineWidth(context, 4);
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineCap(context, kCGLineCapRound);
        
        CGContextAddPath(context, CGPathCreateWithRoundedRect(mapRect, 16, 16, NULL));
        CGContextReplacePathWithStrokedPath(context);
        CGContextClip(context);

        // Define the start and end points for the gradient
        // This determines the direction in which the gradient is drawn
        CGPoint startPoint = CGPointMake(CGRectGetMidX(mapRect), CGRectGetMinY(mapRect));
        CGPoint endPoint = CGPointMake(CGRectGetMidX(mapRect), CGRectGetMaxY(mapRect));
        
        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGGradientRelease(gradient), gradient = NULL;
    }
    

相关文章:

  • 2022-01-17
  • 2022-01-19
  • 2022-01-18
  • 2022-01-08
  • 2021-08-04
  • 2022-02-11
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2022-01-03
  • 2021-12-16
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-12
相关资源
相似解决方案