圆形UIView(带有cornerRadius)没有混合层

use*_*702 10 objective-c layer cornerradius uiview ios

我试图让下面的圆圈有一个不透明的纯白色,其中cornerRadius切出了UIView.

UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(i * (todaySize + rightMargin), 0, smallSize, smallSize)];
circle.layer.cornerRadius = smallSize/2;
circle.layer.borderWidth = 0.5;
circle.layer.backgroundColor = [UIColor whiteColor].CGColor;
circle.backgroundColor = [UIColor whiteColor];
[self addSubview:circle];
Run Code Online (Sandbox Code Playgroud)

我尝试过一些事情,比如设置backgroundColor和opaque而没有任何运气.颜色混合图层仍然显示圆圈的周围是透明的.有人知道如何解决这个问题吗?

Sea*_*dek 14

为避免在使用圆角时进行混合,需要在drawRect中进行舍入,而不是作为图层上的属性.我需要在我正在处理的应用程序中使用圆角背景的UICollectionView单元格.当我使用layer.cornerRadius时,性能受到了极大的打击.打开颜色混合层产生以下结果:

混合细胞

不是我所希望的,我希望那些细胞是绿色的,表明没有发生混合.为此,我将UIView子类化为RoundedCornerView.我的实施是短暂而甜蜜的:

import UIKit

class RoundedCornerView: UIView {
    static let cornerRadius = 40.0 as CGFloat

    override func drawRect(rect: CGRect) {
        let borderPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: RoundedCornerView.cornerRadius)
        UIColor.whiteColor().set()
        borderPath.fill()
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我将我正在舍入的视图设置为我的笔尖中的RoundedCornerView.在这一点上跑步产生了这样的结果:

非混合细胞

滚动是黄油平滑的,不再发生任何混合.一个奇怪的副作用是视图的backgroundColor属性将为角的排除区域着色,而不是视图的主体.这意味着backgroundColor应设置为视图后面的任何内容,而不是所需的填充颜色.