绘制代码如下
CGFloat R0 = 0.267, G0 = 0.303, B0 = 0.335;
CGFloat R1 = 0.040, G1 = 0.040, B1 = 0.040;
UIColor *tintColor = [KxMenu tintColor];
if (tintColor) {
CGFloat a;
[tintColor getRed:&R0 green:&G0 blue:&B0 alpha:&a];
}
CGFloat X0 = frame.origin.x;
CGFloat X1 = frame.origin.x + frame.size.width;
CGFloat Y0 = frame.origin.y;
CGFloat Y1 = frame.origin.y + frame.size.height;
// render arrow
UIBezierPath *arrowPath = [UIBezierPath bezierPath];
// fix the issue with gap of arrow's base if on the edge
const CGFloat kEmbedFix = 3.f;
if (_arrowDirection == KxMenuViewArrowDirectionUp) {
const CGFloat arrowXM = _arrowPosition;
const CGFloat arrowX0 = arrowXM - kArrowSize;
const CGFloat arrowX1 = arrowXM + kArrowSize;
const CGFloat arrowY0 = Y0;
const CGFloat arrowY1 = Y0 + kArrowSize + kEmbedFix;
[arrowPath moveToPoint: (CGPoint){arrowXM, arrowY0}];
[arrowPath addLineToPoint: (CGPoint){arrowX1, arrowY1}];
[arrowPath addLineToPoint: (CGPoint){arrowX0, arrowY1}];
[arrowPath addLineToPoint: (CGPoint){arrowXM, arrowY0}];
[[UIColor colorWithRed:R0 green:G0 blue:B0 alpha:1] set];
Y0 += kArrowSize;
} else if (_arrowDirection == KxMenuViewArrowDirectionDown) {
const CGFloat arrowXM = _arrowPosition;
const CGFloat arrowX0 = arrowXM - kArrowSize;
const CGFloat arrowX1 = arrowXM + kArrowSize;
const CGFloat arrowY0 = Y1 - kArrowSize - kEmbedFix;
const CGFloat arrowY1 = Y1;
[arrowPath moveToPoint: (CGPoint){arrowXM, arrowY1}];
[arrowPath addLineToPoint: (CGPoint){arrowX1, arrowY0}];
[arrowPath addLineToPoint: (CGPoint){arrowX0, arrowY0}];
[arrowPath addLineToPoint: (CGPoint){arrowXM, arrowY1}];
[[UIColor colorWithRed:R1 green:G1 blue:B1 alpha:1] set];
Y1 -= kArrowSize;
} else if (_arrowDirection == KxMenuViewArrowDirectionLeft) {
const CGFloat arrowYM = _arrowPosition;
const CGFloat arrowX0 = X0;
const CGFloat arrowX1 = X0 + kArrowSize + kEmbedFix;
const CGFloat arrowY0 = arrowYM - kArrowSize;;
const CGFloat arrowY1 = arrowYM + kArrowSize;
[arrowPath moveToPoint: (CGPoint){arrowX0, arrowYM}];
[arrowPath addLineToPoint: (CGPoint){arrowX1, arrowY0}];
[arrowPath addLineToPoint: (CGPoint){arrowX1, arrowY1}];
[arrowPath addLineToPoint: (CGPoint){arrowX0, arrowYM}];
[[UIColor colorWithRed:R0 green:G0 blue:B0 alpha:1] set];
X0 += kArrowSize;
} else if (_arrowDirection == KxMenuViewArrowDirectionRight) {
const CGFloat arrowYM = _arrowPosition;
const CGFloat arrowX0 = X1;
const CGFloat arrowX1 = X1 - kArrowSize - kEmbedFix;
const CGFloat arrowY0 = arrowYM - kArrowSize;;
const CGFloat arrowY1 = arrowYM + kArrowSize;
[arrowPath moveToPoint: (CGPoint){arrowX0, arrowYM}];
[arrowPath addLineToPoint: (CGPoint){arrowX1, arrowY0}];
[arrowPath addLineToPoint: (CGPoint){arrowX1, arrowY1}];
[arrowPath addLineToPoint: (CGPoint){arrowX0, arrowYM}];
[[UIColor colorWithRed:R1 green:G1 blue:B1 alpha:1] set];
X1 -= kArrowSize;
}
[arrowPath fill];
// render body
const CGRect bodyFrame = {X0, Y0, X1 - X0, Y1 - Y0};
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:bodyFrame
cornerRadius:8];
const CGFloat locations[] = {0, 1};
const CGFloat components[] = {
R0, G0, B0, 1,
R1, G1, B1, 1,
};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,
components,
locations,
sizeof(locations)/sizeof(locations[0]));
CGColorSpaceRelease(colorSpace);
[borderPath addClip];
CGPoint start, end;
if (_arrowDirection == KxMenuViewArrowDirectionLeft ||
_arrowDirection == KxMenuViewArrowDirectionRight) {
start = (CGPoint){X0, Y0};
end = (CGPoint){X1, Y0};
} else {
start = (CGPoint){X0, Y0};
end = (CGPoint){X0, Y1};
}
CGContextDrawLinearGradient(context, gradient, start, end, 0);
CGGradientRelease(gradient);