ios view 切上部分圆角_ios – 具有圆角的UIView:如何正确剪辑子视图?
我創建了一個覆蓋drawRect的UIView子類,并使用AddArcToPoint()繪制圓角. (我不想使用圖層的角半徑屬性,因為我需要定義哪些角必須舍入.)
問題我不能得到:如果我添加一個子視圖(0 | 0),它隱藏我的圓角.任何想法如何解決這個問題?我想要很好地剪輯.
這是繪制圓角矩形的代碼.這是Monotouch,但任何開發人員都應該可以理解.
public override void Draw (RectangleF rect)
{
using (var oContext = UIGraphics.GetCurrentContext())
{
oContext.SetLineWidth (this.StrokeWidth);
oContext.SetStrokeColor (this.oStrokeColor.CGColor);
oContext.SetFillColor (this.oRectColor.CGColor);
RectangleF oRect = this.Bounds;
float fRadius = this.CornerRadius;
float fWidth = oRect.Width;
float fHeight = oRect.Height;
// Make sure corner radius isn't larger than half the shorter side.
if (fRadius > fWidth / 2.0f)
{
fRadius = fWidth / 2.0f;
}
if (fRadius > fHeight / 2.0f)
{
fRadius = fHeight / 2.0f;
}
float fMinX = oRect.GetMinX ();
float fMidX = oRect.GetMidX ();
float fMaxX = oRect.GetMaxX ();
float fMinY = oRect.GetMinY ();
float fMidY = oRect.GetMidY ();
float fMaxY = oRect.GetMaxY ();
// Move to left middle.
oContext.MoveTo (fMinX,fMidY);
// Arc to top middle.
oContext.AddArcToPoint (fMinX,fMinY,fMidX,(this.RoundCorners & ROUND_CORNERS.TopLeft) == ROUND_CORNERS.TopLeft ? fRadius : 0);
// Arc to right middle.
oContext.AddArcToPoint (fMaxX,fMaxX,fMidY,(this.RoundCorners & ROUND_CORNERS.TopRight) == ROUND_CORNERS.TopRight ? fRadius : 0);
// Arc to bottom middle.
oContext.AddArcToPoint (fMaxX,fMaxY,(this.RoundCorners & ROUND_CORNERS.BottomRight) == ROUND_CORNERS.BottomRight ? fRadius : 0);
// Arc to left middle.
oContext.AddArcToPoint (fMinX,fMinX,(this.RoundCorners & ROUND_CORNERS.BottomLeft) == ROUND_CORNERS.BottomLeft ? fRadius : 0);
// Draw the path.
oContext.ClosePath ();
oContext.DrawPath (CGPathDrawingMode.FillStroke);
}
}
編輯:
這是一段代碼,演示如何使用CALayer解決它.
private void UpdateMask()
{
UIBezierPath oMaskPath = UIBezierPath.FromRoundedRect (this.Bounds,this.eRoundedCorners,new SizeF (this.fCornerRadius,this.fCornerRadius));
CAShapeLayer oMaskLayer = new CAShapeLayer ();
oMaskLayer.Frame = this.Bounds;
oMaskLayer.Path = oMaskPath.CGPath;
this.Layer.Mask = oMaskLayer;
}
總結
以上是生活随笔為你收集整理的ios view 切上部分圆角_ios – 具有圆角的UIView:如何正确剪辑子视图?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三目童子攻略
- 下一篇: 提取图片纹理_Fundamentals