我想使一个UIView或的UIImageView是一个圆。或圆形,我可以改变使用滑块的尺寸,并与一个pickerview颜色。

有帮助吗?

解决方案

我可以至少你显示用于绘制任意大小的圆的快捷方式。没有OpenGL中,无芯图形绘制需要的。

导入QuartzCore框架来获取访问的 .cornerRadius 您的UIView或UIImageView的财产。

#import <QuartzCore/QuartzCore.h>

也可手动将它添加到你的项目的框架文件夹。

此方法添加到您的视图控制器或任何你需要的:

-(void)setRoundedView:(UIImageView *)roundedView toDiameter:(float)newSize;
{
    CGPoint saveCenter = roundedView.center;
    CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
    roundedView.frame = newFrame;
    roundedView.layer.cornerRadius = newSize / 2.0;
    roundedView.center = saveCenter;
}

要使用它,只传递一个的UIImageView 和直径。这个例子假设你有一个名为“中国保监会”的UIImageView添加作为一个子视图到您的视图的。它应该有一个的的backgroundColor 的设定,所以你可以看到它。

circ.clipsToBounds = YES;
[self setRoundedView:circ toDiameter:100.0];

这只是处理的的UIImageViews 的,但您可以将其推广到所有的的UIView 的。

注意:由于iOS的7,clipToBounds需要YES

其他提示

// For those looking to round the corners of an image view
imageView.layer.cornerRadius = imageView.bounds.size.width/2;
imageView.layer.masksToBounds = YES;

如果有人正在寻找夫特当量比下面是答案(Xcode7.2):

(适用为此工作的高度和宽度必须相等。)

extension UIView {
    func makeCircular() {
        self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) / 2.0
        self.clipsToBounds = true
    }
}

“进入图像的描述在这里“

无需图形调用。只是拐角半径设定为宽度/ 2。这可以在任何视觉对象来完成,即UI元件

正如一些评论所提到的,@IBDesignable使这更容易了,所以你可以使用Interface Builder来配置圆润的UIImageView。

首先创建一个名为类和RoundedImageView.swift这个代码粘贴到它:

import UIKit

@IBDesignable public class RoundedImageView: UIImageView {

    override public func layoutSubviews() {
        super.layoutSubviews()

        //hard-coded this since it's always round
        layer.cornerRadius = 0.5 * bounds.size.width
    }
}

选择InterfaceBuilder下的的UIImageView和从的UIImageView类改变到自定义RoundedImageView:

“在这里输入的图像描述”

设置Clip to Bounds为真(或PIC将延伸超过圆):

“在这里输入的图像描述”

现在应当一轮本身InterfaceBuilder下,这是非常漂亮的在那里。被确保的宽度和高度设定为相同的值或它会被成形为类似一个飞艇!

“在这里输入的图像描述”

您需要做一个透明的UIView(0背景颜色阿尔法),然后在其drawRect中使用:, CoreGraphics的通话绘制你的圈子。你也可以编辑视图的层,并给它一个cornerRadius。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top