簡體   English   中英

iOS:在UIView中繪制陰影

[英]iOS: Draw shadow within UIView

我找到了一些很棒的代碼,它是UIView的子類,它用陰影繪制該視圖。 然后,我可以在要放置陰影的任何地方使用該視圖:

-(void) layoutSubviews {
    CGFloat coloredBoxMargin = 40;
    CGFloat coloredBoxHeight = self.frame.size.height;
    _coloredBoxRect = CGRectMake(coloredBoxMargin, 0, 40, coloredBoxHeight);
}

-(void) drawRect:(CGRect)rect {

    CGColorRef lightColor =  [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8].CGColor;
    CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4].CGColor;   

    CGContextRef context = UIGraphicsGetCurrentContext();
    // Draw shadow
    CGContextSaveGState(context);
    CGContextSetFillColorWithColor(context, lightColor);
    CGContextSetShadowWithColor(context, CGSizeMake(-13, 0), 10, shadowColor);

    CGContextFillRect(context, _coloredBoxRect);
    CGContextRestoreGState(context);
}

使用以下內容創建:

UIViewWithShadowRight* verticalLineView2 = [[[UIViewWithShadowRight alloc] initWithFrame:CGRectMake(60, 0, 40 , self.view.frame.size.height)] autorelease];
[verticalLineView2 setBackgroundColor:[UIColor clearColor]];
[verticalLineView2 setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[verticalLineView2 setClipsToBounds:NO];
[self.view addSubview:verticalLineView2];
[self.view bringSubviewToFront:verticalLineView2];

問題是,陰影是從右向左繪制的,我該如何反轉並從左向右繪制呢?

使13正而不是負。 這是繪制陰影的x偏移,負值在原始圖形的左側,正值在右側。

嘗試更改此CGContextSetShadowWithColor(context,CGSizeMake(-13,0),10,shadowColor); 將當前的x值(-13)替換為右側的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM