【问题标题】:How to AspectFit a UIBezierPath in iOS using Objective-C如何使用 Objective-C 在 iOS 中调整 UIBezierPath
【发布时间】:2018-09-04 11:42:28
【问题描述】:

我正在尝试 AspectFit 基于 UIBezierPath 的自定义按钮。

这是我在自定义类中的代码,它是 UIButton 类的子类

-(id)initWithCoder:(NSCoder *)coder {
   if (self = [super initWithCoder:coder]) {
    self.contentMode = UIViewContentModeScaleAspectFill;
   }
   return self;
}

-(void)drawRect:(CGRect)rect {

 UIColor* fillColor = [UIColor colorWithRed: 0.451 green: 0.855 blue: 1 alpha: 1];

UIBezierPath* clipPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0.01, 81.6, 40.9)];
[clipPath addClip];

UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(81.21, 13.63)];
[bezierPath addCurveToPoint: CGPointMake(48.11, 40.91) controlPoint1: CGPointMake(84.3, 37.04) controlPoint2: CGPointMake(68.27, 40.91)];
[bezierPath addCurveToPoint: CGPointMake(4.72, 45.04) controlPoint1: CGPointMake(33.83, 40.91) controlPoint2: CGPointMake(3.32, 40.91)];
[bezierPath addCurveToPoint: CGPointMake(20.19, 7.94) controlPoint1: CGPointMake(-1.03, 14.37) controlPoint2: CGPointMake(-1.26, 0.28)];
[bezierPath addCurveToPoint: CGPointMake(81.21, 13.63) controlPoint1: CGPointMake(40.97, 1.57) controlPoint2: CGPointMake(78.62, -6.04)];
[bezierPath closePath];
[fillColor setFill];
[bezierPath fill];
}

在 xib 中,我将自定义类分配给 UIButton 对象。我尝试在 IB 中使用 AspectFit 处理程序,并在 initWithCoder 中尝试过,但似乎没有任何效果。

【问题讨论】:

    标签: ios objective-c uibezierpath


    【解决方案1】:

    最简单的方法可能是在绘制 BezierPath 之前对其应用 aspectFit 变换,或者对图形上下文应用相同的变换。类似于以下内容:

    - (CGAffineTransform) aspectFitFrom:(CGRect)originalRect to:(CGRect)targetRect
    {
        CGFloat xScale = targetRect.size.width / originalRect.size.width;
        CGFloat yScale = targetRect.size.height / originalRect.size.height;
        CGFloat scale = MIN(xScale, yScale);
    
        CGFloat xShift = CGRectGetMidX(targetRect) - CGRectGetMidX(originalRect) * scale;
        CGFloat yShift = CGRectGetMidY(targetRect) - CGRectGetMidY(originalRect) * scale;
        return CGAffineTransformMake(scale, 0, 0, scale, xShift, yShift);
    }
    

    将其应用于贝塞尔路径的边界框和目标的边界。然后在填充之前对 bezierPath 应用变换,如下:

    CGRect pathBBox = bezierPath.bounds; // the bounding box of your bezierPath
    CGAffineTransform transform = [self aspectFitFrom:pathBBox to:rect];
    [bezierPath applyTransform:transform];
    

    当它被缩放时,你需要自己思考你想对形状的线条粗细做些什么。

    【讨论】:

    • 谈到我的代码,我将如何应用这种转换?对象未以编程方式绘制在视图中。
    • 您的对象以编程方式绘制在视图中。这就是 drawRect: 所做的。
    【解决方案2】:

    我仔细估计了图纸的宽度和高度,它给了我 82 x 42 ponts。然后我在代码中应用了比例因子,这样如果在 xib 中缩放按钮大小,则绘图可以适合按钮。

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, rect.size.width / 82, rect.size.height/ 42);
    

    现在完整的代码如下所示。

    - (void)drawRect:(CGRect)rect {
    
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextScaleCTM(context, rect.size.width / 82, rect.size.height/ 42);
    
      {
    
      UIColor* fillColor = [UIColor colorWithRed: 0.451 green: 0.855 blue: 1 alpha: 1];
    
    
      UIBezierPath* clipPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0.01, 81.6, 40.9)];
      [clipPath addClip];
    
      UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    
    
      [bezierPath moveToPoint: CGPointMake(81.21, 13.63)];
      [bezierPath addCurveToPoint: CGPointMake(48.11, 40.91) controlPoint1: CGPointMake(84.3, 37.04) controlPoint2: CGPointMake(68.27, 40.91)];
      [bezierPath addCurveToPoint: CGPointMake(0.72, 25.04) controlPoint1: CGPointMake(33.83, 40.91) controlPoint2: CGPointMake(3.32, 40.91)];
      [bezierPath addCurveToPoint: CGPointMake(20.19, 0.94) controlPoint1: CGPointMake(-1.03, 14.37) controlPoint2: CGPointMake(-1.26, 0.28)];
      [bezierPath addCurveToPoint: CGPointMake(81.21, 13.63) controlPoint1: CGPointMake(40.97, 1.57) controlPoint2: CGPointMake(78.62, -6.04)];
      [bezierPath closePath];
      [fillColor setFill];
      [bezierPath fill];
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 2015-02-09
      相关资源
      最近更新 更多