Jab*_*Jab 52 iphone geometry uiview uiimageview quartz-core
我想制作一个圆形的UIView或UIImageView.或者我可以改变使用滑块大小的圆圈,以及带有选择器视图的颜色.
wil*_*lc2 106
我至少可以向您展示绘制任意大小圆圈的快捷方式.没有OpenGL,不需要核心图形绘图.
导入QuartzCore框架以访问UIView或UIImageView 的.cornerRadius属性.
#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)
还可以手动将其添加到项目的Frameworks文件夹中.
将此方法添加到视图控制器或您需要的任何位置:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
要使用它,只需传递一个UIImageView和一个直径.此示例假定您将一个名为"circ"的UIImageView添加为视图的子视图.它应该有一个backgroundColor设置,以便您可以看到它.
circ.clipsToBounds = YES;
[self setRoundedView:circ toDiameter:100.0];
Run Code Online (Sandbox Code Playgroud)
这只是处理UIImageViews,但你可以将它推广到任何UIView.
注意:自iOS 7起,clipToBounds需要YES.
Joh*_*rck 32
// For those looking to round the corners of an image view
imageView.layer.cornerRadius = imageView.bounds.size.width/2;
imageView.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
पवन*_*पवन 23
如果有人在寻找Swift等价物而不是下面的答案(Xcode7.2):
(为此,工作高度和宽度必须相等.)
extension UIView {
func makeCircular() {
self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) / 2.0
self.clipsToBounds = true
}
}
Run Code Online (Sandbox Code Playgroud)
如某些评论中所述,@ 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
}
}
Run Code Online (Sandbox Code Playgroud)
在InterfaceBuilder中选择UIImageView并将类从UIImageView更改为自定义RoundedImageView:
设置Clip to Bounds为true(否则图片将超出圆圈):
现在,它应该在InterfaceBuilder中四舍五入,这非常漂亮。请确保将宽度和高度设置为相同的值,否则形状会像齐柏林飞艇一样!
一个整洁、干净和优雅的解决方案是在 Xcode 的 Identity Inspector 中创建一个名为的类RoundedUIView并将其选择为UIView元素的自定义类,如所附屏幕截图所示。
import UIKit
@IBDesignable public class RoundedUIView: UIView {
override public func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = self.frame.width / 2;
self.layer.masksToBounds = true
}
}
Run Code Online (Sandbox Code Playgroud)
由于应用程序本身具有绿色主题,因此我需要在白色背景的各种屏幕上显示多个彩色图标。所以我把我UIImgeView放在上面,RoundedUIView让它们看起来像这样:
| 归档时间: |
|
| 查看次数: |
58032 次 |
| 最近记录: |