如何制作圆形UIView [英] How to make a circular UIView

查看:160
本文介绍了如何制作圆形UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个圆形的UIView或UIImageView。或者我可以改变使用滑块大小的圆圈,以及带有选择器视图的颜色。

I want to make a UIView or UIImageView that is a circle. Or a circle that i can change the size of using a slider, and the color of with a pickerview.

推荐答案

我可以在至少显示绘制任意大小圆圈的快捷方式。没有OpenGL,不需要核心图形绘图。

I can at least show you a shortcut for drawing circles of arbitrary size. No OpenGL, no Core Graphics drawing needed.

导入QuartzCore框架以访问UIView或UIImageView的 .cornerRadius 属性。

Import the QuartzCore framework to get access to the .cornerRadius property of your UIView or UIImageView.

#import <QuartzCore/QuartzCore.h>

还可以手动将其添加到项目的Frameworks文件夹中。

Also manually add it to your project's Frameworks folder.

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

Add this method to your view controller or wherever you need it:

-(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 和直径。此示例假定您将名为circ的UIImageView添加为视图的子视图。它应该设置 backgroundColor ,以便您可以看到它。

To use it, just pass it a UIImageView and a diameter. This example assumes you have a UIImageView named "circ" added as a subview to your view. It should have a backgroundColor set so you can see it.

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

这只是处理 UIImageViews 但你可以将它推广到任何 UIView

This just handles UIImageViews but you can generalize it to any UIView.

注意:自iOS 7起,clipToBounds需要为YES。

NOTE: Since iOS 7, clipToBounds need to YES.

这篇关于如何制作圆形UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆