簡體   English   中英

圓形UIView(帶有cornerRadius)沒有混合層

[英]Circular UIView (with cornerRadius) without Blended Layer

我試圖讓下面的圓圈有一個不透明的純白色,其中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];

我嘗試過一些事情,比如設置backgroundColor和opaque而沒有任何運氣。 顏色混合圖層仍然顯示圓圈的周圍是透明的。 有人知道如何解決這個問題嗎?

為避免在使用圓角時進行混合,需要在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()
    }
}

然后我將我正在舍入的視圖設置為我的筆尖中的RoundedCornerView。 在這一點上跑步產生了這樣的結果:

非混合細胞

滾動是黃油平滑的,不再發生任何混合。 一個奇怪的副作用是視圖的backgroundColor屬性將為角的排除區域着色,而不是視圖的主體。 這意味着backgroundColor應設置為視圖后面的任何內容,而不是所需的填充顏色。

嘗試使用遮罩來避免混合和處理父/子背景顏色匹配。

override func layoutSubviews() {
    super.layoutSubviews()

    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 20).cgPath
    layer.mask = maskLayer
}

clipsToBounds上的clipsToBoundsmasksToBounds上的masksToBounds設置為YES

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM