【问题标题】:Overlapping transparent UIViews重叠的透明 UIView
【发布时间】:2014-12-09 11:15:10
【问题描述】:

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

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

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

http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html 是我正在阅读的内容,我找到了用于绘图的 CGBlendMode,如果涉及到使用它(尽管我宁愿不这样做!)

【问题讨论】:

    标签: ios objective-c uiview core-graphics compositing


    【解决方案1】:

    您无法在 iOS 上控制视图的合成模式(或者,实际上,CALayers)。

    我能想到的最佳解决方案是让两个视图都使用clearColor(或nil)背景,并使用单个CAShapeLayer 来绘制两者的背景。如果您的两个视图具有相同的父级,那就不太难了。

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

    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
    

    【讨论】:

      【解决方案2】:

      如果您将它们都添加到同一个父级UIView,告诉UIView 进行光栅化,然后在父级上设置 alpha,您将获得所需的效果。我不确定这是否符合您的显示结构或性能需求。

      UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
      [parent setBackgroundColor:[UIColor clearColor]];
      [parent.layer setShouldRasterize:YES];
      [parent.layer setRasterizationScale:[[UIScreen mainScreen] scale]];
      [parent setAlpha:0.25];
      UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 125, 125)];
      [subview1 setBackgroundColor:[UIColor whiteColor]];
      [parent addSubview:subview1];
      UIView *subview2 = [[UIView alloc] initWithFrame:CGRectMake(75, 75, 125, 125)];
      [subview2 setBackgroundColor:[UIColor whiteColor]];
      [parent addSubview:subview2];
      

      【讨论】:

      • 您假设他想要在 alpha 0.25 处查看视图的全部内容,但这与仅在 alpha 0.25 处拥有 backgroundColor 不同。
      • 实际上,我的意思是他可以只添加背景,然后将内容(以不同的层次结构)与背景同步。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      相关资源
      最近更新 更多