iOS 흰색에서 투명 그라데이션 계층은 회색입니다.
앱 하단에 팝업되는 이 작은 세부 보기 하단에 CA Gradient Layer가 삽입되어 있습니다.보시다시피 흰색에서 선명한 색상으로 설정했는데 이상한 회색 색조가 나타나고 있습니다.아이디어 있어요?
// Set up detail view frame and gradient
[self.detailView setFrame:CGRectMake(0, 568, 320, 55)];
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = self.detailView.bounds;
layer.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
layer.startPoint = CGPointMake(1.0f, 0.7f);
layer.endPoint = CGPointMake(1.0f, 0.0f);
[self.detailView.layer insertSublayer:layer atIndex:0];
문제가 되는 견해는 다음과 같습니다.
clearColor는 알파가 0인 검은색 채널을 가지고 있어서 사용해야 했습니다.
[UIColor colorWithWhite:1 alpha:0]
그리고 그것은 잘 작동합니다.
스위프트에서 이것은 나에게 효과가 있었습니다.
UIColor.white.withAlphaComponent(0).cgColor
다른 색들은 이렇게 작동할 것이라는 점을 지적할 가치가 있습니다.위의 두 가지 대답을 조합하여 사용하면...
목표 C
UIColor *colour = [UIColor redColor];
NSArray *colourArray = @[(id)[colour colorWithAlphaComponent:0.0f].CGColor,(id)colour.CGColor]
NSArray *locations = @[@0.2,@0.8];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = colourArray;
gradientLayer.locations = locations;
gradientLayer.frame = self.frame;
[self.layer addSublayer:gradientLayer];
스위프트 3
let colour:UIColor = .red
let colours:[CGColor] = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]
let locations:[NSNumber] = [0.2,0.8]
let gradientLayer = CAGradientLayer()
gradientLayer.colors = colours
gradientLayer.locations = locations
gradientLayer.frame = frame
layer.addSublayer(gradientLayer)
Swift 3 구문,
UIColor(white: 1, alpha: 0).cgColor
위의 답변은 다음과 같습니다.
UIColor.white.withAlphaComponent(0).cgColor
그리고.
UIColor(white: 1.0, alpha: 0.0).cgColor
(OP가 가리키는 회색이 아니라) 그라데이션의 일부를 명확하게 하는 측면에서 작동해야 합니다.그러나 선명한 색상이 표시되어야 할 때 투명한 흰색이 계속 표시되는 경우에는backgroundColor
그라데이션을 적용할 뷰도 명확합니다.
기본적으로 해당 보기의 배경색은 흰색(또는 장치가 어두운 모드인 경우 어두운 색)이 될 수 있으므로 그라데이션을 적용할 때 해당 보기의 선명한 부분이 "차단"됩니다.backgroundColor
그 자체로그것을 클리어하도록 설정하면 당신은 갈 수 있을 것입니다.
그만큼, 저는 투명한 흰색을 사용했음에도 불구하고 여전히 회색을 띠고 있습니다.
그래서 저는 제 접근 방식을 바꾸고 그라데이션보다는 마스크를 선택했습니다.최종 결과는 동일하고, 음, 더 낫습니다. 왜냐하면 이것은 단지 적절한 배경을 가진 것만이 아니라 모든 상황에서 작동하기 때문입니다.
나는 IB와 함께 이 코드를 시도하지 않았지만, 그것이 작동하기를 바랍니다.설정하기backgroundColor
그리고 당신은 가도 좋습니다.
@IBDesignable
class FadingView: UIView {
@IBInspectable var startLocation: Double = 0.05 { didSet { updateLocations() }}
@IBInspectable var endLocation: Double = 0.95 { didSet { updateLocations() }}
@IBInspectable var horizontalMode: Bool = false { didSet { updatePoints() }}
@IBInspectable var diagonalMode: Bool = false { didSet { updatePoints() }}
@IBInspectable var invertMode: Bool = false { didSet { updateColors() }}
private let gradientLayerMask = CAGradientLayer()
private func updatePoints() {
if horizontalMode {
gradientLayerMask.startPoint = diagonalMode ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5)
gradientLayerMask.endPoint = diagonalMode ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5)
} else {
gradientLayerMask.startPoint = diagonalMode ? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0)
gradientLayerMask.endPoint = diagonalMode ? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1)
}
}
private func updateLocations() {
gradientLayerMask.locations = [startLocation as NSNumber, endLocation as NSNumber]
}
private func updateSize() {
gradientLayerMask.frame = bounds
}
private func updateColors() {
gradientLayerMask.colors = invertMode ? [UIColor.white.cgColor, UIColor.clear.cgColor] : [UIColor.clear.cgColor, UIColor.white.cgColor]
}
private func commonInit() {
layer.mask = gradientLayerMask
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
updatePoints()
updateLocations()
updateSize()
updateColors()
}
}
iOS 13에서 명암 모드를 기반으로 한 흰색/검은색(또는 밝은/어두운 모양의 모든 색상) 그라데이션을 처리하기 위해 이 접근 방식은 새로운 시스템 색상에도 적용됩니다.
gradientLayer.colors = [
UIColor.systemBackground.cgColor,
UIColor.systemBackground.withAlphaComponent(0).cgColor]
언급URL : https://stackoverflow.com/questions/24882361/ios-white-to-transparent-gradient-layer-is-gray
'programing' 카테고리의 다른 글
스프링 부트json 본문의 일부로 멀티파트 파일 업로드 (0) | 2023.08.24 |
---|---|
PowerShell 노트 속성이란? (0) | 2023.08.24 |
JSON 파일을 변환할 때 "Invalid JSON 프리미티브" 오류가 발생함 (0) | 2023.08.24 |
MySQL TIMESTAMP를 CURRENT_TIMESTAMP가 아닌 기본 NULL로 설정 (0) | 2023.08.24 |
Git는 Gtk-WARNING을 생성합니다: 디스플레이를 열 수 없습니다. (0) | 2023.08.24 |