子类UIImageView
以允许它通过UIBezierPath
.
示例UIView
:
public class UIViewCustomRounded: UIView
{
// Inspired by:
// https://stackoverflow.com/questions/4847163/round-two-corners-in-uiview
// https://stackoverflow.com/questions/2264083/rounded-uiview-using-calayers-only-some-corners-how/
public UIViewCustomRounded (): base()
{
}
/// <summary>
/// Sets the corners.
/// </summary>
/// <param name="corners">Corners.</param>
/// <param name="cornerRadius">Corner radius.</param>
public void SetCorners(UIRectCorner corners, float cornerRadius)
{
var maskLayer = new CAShapeLayer();
maskLayer.Frame = Bounds;
var roundedPath = UIBezierPath.FromRoundedRect(maskLayer.Bounds, corners, new SizeF(cornerRadius, cornerRadius));
// maskLayer.FillColor = UIColor.White.CGColor;
// maskLayer.BackgroundColor = UIColor.Clear.CGColor;
maskLayer.Path = roundedPath.CGPath;
//Don't add masks to layers already in the hierarchy!
Layer.Mask = maskLayer;
}
}
用法:
var a = new UIViewCustomRounded()
// Don't forget to set frame and add it as subview
a.SetCorners(UIRectCorner.BottomLeft | UIRectCorner.BottomRight, 10);
只要UIImageView
是 的子类UIView
,它也应该适用UIImageView
。
参考答案。