我找到了一些很好的代码,它是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];
问题是,阴影是从右到左绘制的,如何将其反转并从左到右绘制?
答案 0 :(得分:1)
使13
为正而非为负。这是绘制阴影的x偏移量,负值位于原始图形的左侧,正值位于右侧。
答案 1 :(得分:0)
尝试更改此CGContextSetShadowWithColor(context,CGSizeMake(-13,0),10,shadowColor); 将当前x值(-13)替换为右侧的值。