不使用图像的自定义UIButton形状
问题描述:
我想创建一个与UIBackBarButtonItem类似的UIButton(带有箭头指向左侧的导航堆栈,如果可能的话,我宁愿这样做而不必使用图像,因为按钮会根据手机的方向有不同的尺寸。不使用图像的自定义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];
选中此http://stackoverflow.com/questions/6184321/where-can-i-find-some-beautiful-iphone-buttons/6184393#6184393 – Jhaliya 2011-05-31 19:07:03
它解决了什么问题? – 2012-03-19 08:15:19
你是什么意思? – 2012-03-20 01:10:46