带阴影的视图
问题描述:
我正在寻找像上图(从Groupon应用程序)为视图编写效果的方法。使用下面的代码,我试图得到一个阴影,但它不起作用。我期待的效果是从上到下变暗的视图。有人可以告诉我我做错了什么,以及如何更接近Groupon获得的效果?
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowRadius = 8.0f;
view.layer.shadowOpacity = 0.75f;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:4.0];
view.layer.shadowPath = path.CGPath;
答
只是重申杰西Rusak的解决方案上面,在你的UIView的drawRect创建由清晰到黑色的渐变,并把它添加到您的视图的子层:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * darkerColor = [UIColorcolorWithWhite:0.0alpha:0.5];
CGContextSetFillColorWithColor(context, darkerColor.CGColor);
CGContextFillRect(context, self.bounds);
NSArray *colors = [NSArrayarrayWithObjects:
((id)[self.backgroundColorcolorWithAlphaComponent:0.0f].CGColor),
((id) [UIColorcolorWithWhite:0.0alpha:1.0].CGColor), nil];
gradientLayer = [[CAGradientLayeralloc] init];
gradientLayer.startPoint = CGPointMake(0.5, 0);
gradientLayer.endPoint = CGPointMake(0.5,1);
gradientLayer.frame = self.frame;
gradientLayer.colors = colors;
[self.layerinsertSublayer:gradientLayeratIndex:0];
}
我不知道为什么我越来越下降投票。只是尝试学习新的东西 – tranvutuan 2013-03-03 21:54:59
因为你甚至没有试图自己实现它(或者据我们所知,你没有)。在你问我们为你做这一切之前,向我们展示一些代码。 – CodaFi 2013-03-03 21:58:00
请参阅我的更新OP – tranvutuan 2013-03-03 22:01:47