【问题标题】:How to get a black line on a transparent UIView如何在透明的 UIView 上获得黑线
【发布时间】:2013-10-01 14:54:04
【问题描述】:

我必须在透明的 UIView 上画一条黑线,但是尽管将线的 alpha 设置为 1,但这需要 UIView 的透明度。我该怎么办?

  #import <UIKit/UIKit.h>

 @interface FinalAlgView : UIView{
@private
   //....
   CGFloat lineWidth;
   UIColor *lineColor;
   UIImage *curImage;

CGMutablePathRef path;
  }

 @property (nonatomic, retain) UIColor *lineColor;
 @property (readwrite) CGFloat lineWidth;
 @property (assign, nonatomic) BOOL empty;

实施中

 #define DEFAULT_COLOR [UIColor colorWithRed:127.0/255.0 green:255.0/255.0 blue:0/255.0 alpha:0.6];
  #define DEFAULT_WIDTH 20.0f


  - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {
    self.frame = CGRectMake(0, 0, 1024, 768);
    self.lineWidth = DEFAULT_WIDTH;
    self.lineColor = DEFAULT_COLOR;
    self.empty = YES;
    path = CGPathCreateMutable();

    self.alpha=0.3;
   self.opaque = NO;
   }

   return self;
 }


 - (void)drawRect:(CGRect)rect {
[[UIColor grayColor] set];


UIRectFill(rect);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddPath(context, path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

CGContextStrokePath(context);

self.empty = NO;
}

【问题讨论】:

  • 你是在透明视图上添加黑线视图,还是在超级视图上添加黑线视图?
  • 我在uiview trasparent中添加了黑线
  • 我要标记一行
  • 对不起,我不太明白。您有一个透明的 UIView 并希望在该透明 UIView 之上添加行。你想让你的线结实吗? (alpha = 1)?

标签: uiview drawing alpha


【解决方案1】:

你必须将 UIView 的 alpha 设置放在 drawRect 中而不是 initWithFrame 中

没有

 - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {

    self.alpha=0.3;


  }

return self;
 }

是的

 - (void)drawRect:(CGRect)rect {

    [[UIColor colorWithRed:200.0/225.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.0]set];


  }

【讨论】:

    猜你喜欢
    • 2011-06-04
    • 2023-03-23
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多