将轮廓阴影添加到与iOS 3.0兼容的UIView

时间:2010-10-26 09:07:33

标签: iphone objective-c uiview drawrect dropshadow

我有两个UIViewControllers A和B,它们被添加为AppDelegate中的子视图,其中B位于A之上。

当点击B上的UIButton时,B使用以下代码滑向左侧:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(slideOutFinished)];
simonSaysView.view.frame = CGRectMake(40-BView.view.frame.size.width, BView.view.frame.origin.y, BView.view.frame.size.width, BView.view.frame.size.height);
[UIView commitAnimations];

在右边缘我想要一个20px投影,但我不能使用BView.view.layer.shadow....的阴影属性,因为它只有3.2 / 4.0 +。并且动画的表现非常糟糕,幻灯片非常滞后(它的流畅而没有阴影)。

我正在考虑使用自定义UIView并在drawRect中做一些魔法,但这是可能的还是我只能在视图的范围内绘制?

希望你们中的一些人和女孩能帮助我。

干杯 objneodude

2 个答案:

答案 0 :(得分:3)

使用以下代码解决

// Add drop shadow to the view.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(self.view.frame.size.width, 0, 30, self.view.frame.size.height);
gradientLayer.colors = [NSArray arrayWithObjects:
                        (id)[UIColor blackColor].CGColor,
                        (id)[UIColor clearColor].CGColor,
                        nil];
gradientLayer.startPoint = CGPointMake(-2, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);   
[self.view.layer addSublayer:gradientLayer];

答案 1 :(得分:0)

我遇到了同样的问题,在尝试了各种想法之后,我刚刚在UIView后面添加了一个阴影颜色(灰色),我需要有一个阴影,请参阅代码:(包含的代码为> 3.2.x / 4.x以及。

    /*Shaded/rounded borders on subviews*/
self.viewOne.layer.cornerRadius = 10;

self.viewOne.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewOne.layer.borderWidth = 0.8;

self.viewTwo.layer.cornerRadius = 10;
self.viewTwo.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewTwo.layer.borderWidth = 0.8;

self.viewThree.layer.cornerRadius = 10;
self.viewThree.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewThree.layer.borderWidth = 0.8;

if (SYSTEM_VERSION_LESS_THAN(@"3.2")) {
    self.shadowOne.layer.cornerRadius = 10;
    self.shadowTwo.layer.cornerRadius = 10;
    self.shadowThree.layer.cornerRadius = 10;
}
else{
    //Use QuartzCore to make shadows etc.
    self.viewOne.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewOne.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewOne.layer.shadowOpacity = 0.6;
    self.viewOne.layer.shouldRasterize = YES;
    self.viewOne.layer.shadowRadius = 2;

    self.viewTwo.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewTwo.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewTwo.layer.shadowOpacity = 0.6;
    self.viewTwo.layer.shouldRasterize = YES;
    self.viewTwo.layer.shadowRadius = 2;

    self.viewThree.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewThree.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewThree.layer.shadowOpacity = 0.6;
    self.viewThree.layer.shouldRasterize = YES;
    self.viewThree.layer.shadowRadius = 2;
}