gpt4 book ai didi

ios - 重叠透明 UIView

转载 作者:行者123 更新时间:2023-11-28 21:56:25 25 4
gpt4 key购买 nike

我用不透明度为 25% 的白色 backgroundColor 覆盖两个 UIView。在一小部分,它们相互重叠,这意味着在该区域,它们的不透明度总计为 50%。

我想保持 25% 的不透明度,即使两个 View 重叠,这实际上意味着在那些重叠点,每个 View 的不透明度下降到 12.5%,总计 25%。

我已经对合成做了一些研究,但我不确定这些模式中的哪一种会有所帮助,或者我将如何将它们应用于这两个 UIView 实例的特定部分.

(http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html 是我正在阅读的内容,我找到了用于绘图的 CGBlendMode,如果要使用它的话(尽管我希望尽可能不要使用它!))

最佳答案

您无法控制 iOS 上 View (或者,实际上,CALayer)的合成模式。

这里我能想到的最佳解决方案是让两个 View 都具有clearColor(或nil)背景,并使用单个CAShapeLayer 绘制两者的背景。如果您的两个 View 具有相同的父 View ,则不会太难。

假设父级是 ParentView 类型。覆盖 ParentView 中的 layoutSubviews 以根据需要创建和更新背景层。如果您移动任一 subview ,请务必将 setNeedsLayout 发送到父 View 。

ParentView.h

#import <UIKit/UIKit.h>

@interface ParentView : UIView

@property (nonatomic, strong) IBOutlet UIView *childView0;
@property (nonatomic, strong) IBOutlet UIView *childView1;

@end

ParentView.m

#import "ParentView.h"

@implementation ParentView {
CAShapeLayer *backdrop;
}

- (void)layoutSubviews {
[super layoutSubviews];
[self layoutBackdrop];
}

- (void)layoutBackdrop {
[self createBackdropIfNeeded];
[self arrangeBackdropBehindChildren];
[self setBackdropPath];
}

- (void)createBackdropIfNeeded {
if (backdrop == nil) {
backdrop = [CAShapeLayer layer];
backdrop.fillColor = [UIColor colorWithWhite:1 alpha:0.25].CGColor;
backdrop.fillRule = kCAFillRuleNonZero;
backdrop.strokeColor = nil;
}
}

- (void)arrangeBackdropBehindChildren {
[self.layer insertSublayer:backdrop atIndex:0];
}

- (void)setBackdropPath {
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.childView0.frame];
[path appendPath:[UIBezierPath bezierPathWithRect:self.childView1.frame]];
backdrop.path = path.CGPath;
}

@end

关于ios - 重叠透明 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26364569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com