问题描述
|
我想制作一个与UIBackBarButtonItem相似的UIButton(带有一个指向导航堆栈的左箭头。我希望这样做而不必使用图像,因为按钮可能会有所不同,具体取决于手机) \的方向。
有没有办法在代码中激活这种影响?我的想法是以某种方式使用按钮的CALayer。
谢谢!
编辑
我正在尝试使用@Deepak的建议,但是遇到了问题。我希望按钮的右侧看起来像[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4],而左侧看起来像箭头。我尝试使用addQuadCurvetoPoint:controlPoint方法执行此操作。
我将矩形的角用作控制点,但是路径并不像我期望的那样弯曲。就像我只使用addLinetoPoint:方法一样,它仍然处于困境。我的代码如下。
float radius = 4.0;
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint startPoint = CGPointMake(rect.size.width/5.0,0);
CGPoint pointBeforetopCurve = CGPointMake(rect.size.width - radius,0);
CGPoint topRightCorner = CGPointMake(rect.size.width,0);
CGPoint pointAfterTopCurve = CGPointMake(rect.size.width,0.0-radius);
CGPoint pointBeforeBottomCurve = CGPointMake(rect.size.width,rect.size.height-radius);
CGPoint bottomrightCorner = CGPointMake(rect.size.width,rect.size.height);
CGPoint pointAfterBottomCurve = CGPointMake(rect.size.width - radius,rect.size.height);
CGPoint pointBeforeArrow = CGPointMake(rect.size.width/5.0,rect.size.height);
CGPoint arrowPoint = CGPointMake(0,rect.size.height/2.0);
[path movetoPoint:pointBeforetopCurve];
[path addQuadCurvetoPoint:pointAfterTopCurve controlPoint:topRightCorner];
[path addLinetoPoint:pointBeforeBottomCurve];
[path addQuadCurvetoPoint:pointAfterBottomCurve controlPoint:bottomrightCorner];
[path addLinetoPoint:pointBeforeArrow];
[path addLinetoPoint:arrowPoint];
[path addLinetoPoint:startPoint];
解决方法
您可以使用
Quartz
进行此操作。您将需要子类UIButton
并实现其drawRect:
方法。您将必须定义路径并用渐变填充。
您还必须实现hitTest:withEvent:
,因为它涉及非矩形形状。
, 这为我解决了绘画上的困难
float radius = 10.0;
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint startPoint = CGPointMake(rect.size.width/5.0,0);
CGPoint pointBeforeTopCurve = CGPointMake(rect.size.width - radius,0);
CGPoint topRightCorner = CGPointMake(rect.size.width,0.0);
CGPoint pointAfterTopCurve = CGPointMake(rect.size.width,radius);
CGPoint pointBeforeBottomCurve = CGPointMake(rect.size.width,rect.size.height-radius);
CGPoint bottomRightCorner = CGPointMake(rect.size.width,rect.size.height);
CGPoint pointAfterBottomCurve = CGPointMake(rect.size.width - radius,rect.size.height);
CGPoint pointBeforeArrow = CGPointMake(rect.size.width/5.0,rect.size.height);
CGPoint arrowPoint = CGPointMake(0.0,rect.size.height/2.0);
[path moveToPoint:pointBeforeTopCurve];
[path addQuadCurveToPoint:pointAfterTopCurve controlPoint:topRightCorner];
[path addLineToPoint:pointBeforeBottomCurve];
[path addQuadCurveToPoint:pointAfterBottomCurve controlPoint:bottomRightCorner];
[path addLineToPoint:pointBeforeArrow];
[path addLineToPoint:arrowPoint];
[path addLineToPoint:startPoint];
[path fill];