我正在尝试对基于UIBezierPath的自定义按钮进行AspectFit。
这是我的自定义类中的代码,该类是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中尝试过,似乎没有任何效果。
答案 0 :(得分:0)
最简单的方法可能是在绘制前将AspectFit变换应用于BezierPath,或者将相同的变换应用于图形上下文。类似于以下内容:
- (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);
}
将其应用于Bezier路径的边界框以及目标的边界。然后,在填充之前将转换应用于bezierPath,如下所示:
CGRect pathBBox = bezierPath.bounds; // the bounding box of your bezierPath
CGAffineTransform transform = [self aspectFitFrom:pathBBox to:rect];
[bezierPath applyTransform:transform];
您需要考虑一下缩放后形状的线宽如何处理。
答案 1 :(得分:0)
我仔细地估计了图的宽度和高度,它给了我82 x 42磅。然后,我在代码中应用了比例因子,因此,如果按钮的大小在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];
}
}