iOS中的透明环 [英] Transparent Ring in iOS
问题描述
我的视线中有一个圆形的头像.我是这样的:
I have a circle avatar on my view. I make it like this:
self.imageView.layer.cornerRadius = 75;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderWidth = 1;
self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
但是我想要头像和白色边框之间的透明2px环.我无法在背景上绘制白色圆圈,因为化身可以移动并且戒指位置也会丢失.我的想法是使用
But I want transparent 2px ring between avatar and white border. I can't draw a white circle on the background, because the avatar can move and ring position will be lost. I have the idea to stroke path using
CGContextSetBlendMode(context, kCGBlendModeClear);
在分配了层之后,但是我没有发现它到底是怎么做到的.感谢您的帮助.
after assigning the layer, but I don't catch how exactly do it. Appreciate for your help.
推荐答案
该问题似乎只是缺乏想象力的问题.
that question seems to be a lack-of-imagination issue only.
在 Interface Builder 中,您需要添加一个容器视图,并且该视图包含化身图像视图;这是它在我的屏幕上的外观的原始图片,在编辑器中的视图:
in the Interface Builder, you need to add a container view and that view holds the avatar image view; it is the raw picture of how it look on my screen the view in the editor:
这是视图层次结构中两个视图之间的关系:容器视图是头像图像视图的超级视图:
that is the relationship between the two views in the view hiearchy: the container view is the superview of the avatar image view:
在将相关的插座(分别称为_containerView
和_avatarImageView
)添加到类并将它们连接到视图之后;我们还可以将此小片段添加到我们的代码中:
after adding the related outlets (call them _containerView
and _avatarImageView
) to the class and conntected them to the views; we also can add this little snippet to our code:
[_containerView setBackgroundColor:[UIColor clearColor]];
[_containerView.layer setCornerRadius:MIN(_containerView.bounds.size.width, _containerView.bounds.size.height) / 2.0];
[_containerView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[_containerView.layer setBorderWidth:4.0];
[_avatarImageView.layer setCornerRadius:MIN(_avatarImageView.bounds.size.width, _avatarImageView.bounds.size.height) / 2.0];
[_avatarImageView.layer setMasksToBounds:TRUE];
在模拟器或真实设备violá上运行项目后,透明环出现在图像和边框之间:
and after running the project on the simulator or a real device, violá, the transparent ring appears between the image and the border:
注意:透明环的实际大小取决于化身比容纳它的容器视图小多少.重要的!我不知道女孩是谁,请不要问我她的电话号码.
这篇关于iOS中的透明环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!