【问题标题】:UIImage with rounded corner on left side monotouch iphone左侧单点触控 iphone 上带有圆角的 UIImage
【发布时间】:2013-04-06 09:25:11
【问题描述】:

我想为我的表格视图创建一个单元格,如下图所示:

如您所见,在这个单元格中,我有一个 UIImage,它的左侧有一些圆角半径。我使用此代码将角半径添加到我的 UIImage:

    ThumbnailImg .Layer .CornerRadius = 7.0f;
ThumbnailImg .Layer .MasksToBounds = true ;

但我不知道如何为 uiimage 的 let 侧设置半径。

我猜的另一个解决方案是更改 uiimageview,使其左角与单元格的角匹配(我在每个部分中使用分组表宽度 1 行),因此边框将自动创建。但是对于这种方式,我也不知道我应该做什么:-s。

我也为objective-c找到了这个解决方案。任何人都可以帮助在 monotouch iphone 上使用它吗?

https://stackoverflow.com/a/4930166

【问题讨论】:

  • 我怀疑第二个解决方案是正确的解决方案。但我对此一无所知:-S

标签: iphone uitableview xamarin.ios uiimageview


【解决方案1】:

子类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);

只要UIImageViewUIView 的子类,它也应该适用于UIImageView

Reference answer.

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2011-06-20
    • 2010-09-17
    • 1970-01-01
    • 2012-09-09
    • 2016-08-22
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多