【问题标题】:UIView with transparent in middle中间透明的 UIView
【发布时间】:2014-08-03 12:13:26
【问题描述】:

是否可以创建这样的UIView 填充颜色,但中间是透明的?

我正在考虑在这里创建 5 UIViews。只是想知道是否有可能只使用一个UIView

【问题讨论】:

标签: ios objective-c uiview transparent


【解决方案1】:

Duncan C,我知道我应该从哪里开始,然后我找到了CALayer with transparent hole in it

UIBezierPath *overlayPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
UIBezierPath *transparentPath = [UIBezierPath bezierPathWithRect:CGRectMake(60, 120, 200, 200)];
[overlayPath appendPath:transparentPath];
[overlayPath setUsesEvenOddFillRule:YES];

CAShapeLayer *fillLayer = [CAShapeLayer layer];
fillLayer.path = overlayPath.CGPath;
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor colorWithRed:255/255.0 green:20/255.0 blue:147/255.0 alpha:1].CGColor;

[self.view.layer addSublayer:fillLayer];

使用2个UIBezierPath,然后填充我想要的颜色(我的问题是粉红色),然后添加为子图层

【讨论】:

  • 如果有人知道,如何通过触摸透明 uiview 开始任何触摸移动方法..请帮助我。
  • @milanpatel,不要在其他人的帖子中发布新问题作为评论。创建您自己的问题。
【解决方案2】:

您创建一个视图作为 UIView 的子类并添加以下代码: 在 YourView.h 中

#import <UIKit/UIKit.h>

@interface YourView : UIView

@property (nonatomic, assign) CGRect rectForClearing;
@property (nonatomic, strong) UIColor *overallColor;

@end

在 YourView.m 中

#import "YourView.h"

@implementation NBAMiddleTransparentView

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder // support init from nib
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    CGContextRef ct = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ct, self.overallColor.CGColor);
    CGContextFillRect(ct, self.bounds);
    CGContextClearRect(ct, self.rectForClearing);
}

@end

使用:

yourView.overallColor = [UIColor redColor];
yourView.rectForClearing = CGRectMake(20, 20, 20, 20);

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    是的,这是可能的。您可以将遮罩层附加到视图层,并“打出”遮罩的中心部分。这实际上很容易。

    【讨论】:

    • 此评论需要详细说明如何添加蒙版并“打孔”
    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2015-02-17
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多