2013-03-03 100 views
1

enter image description here帶陰影的視圖

我正在尋找像上圖(從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; 
+0

我不知道爲什麼我越來越下降投票。只是嘗試學習新的東西 – tranvutuan 2013-03-03 21:54:59

+1

因爲你甚至沒有試圖自己實現它(或者據我們所知,你沒有)。在你問我們爲你做這一切之前,向我們展示一些代碼。 – CodaFi 2013-03-03 21:58:00

+0

請參閱我的更新OP – tranvutuan 2013-03-03 22:01:47

回答

3

您正在尋找的效果稱爲漸變。 (在iOS中,陰影是一些物體的重複,模糊,有色和偏移。)

您可以使用CAGradientLayer來獲得所需的效果;嘗試添加其中之一作爲您的視圖的子圖層。

0

只是重申傑西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]; 
}