Search code examples
iphoneobjective-cuiviewdrawrectdropshadow

Adding outline shadow to a UIView which is iOS 3.0 compatible


I have two UIViewControllers A and B and they are added as subviews in the AppDelegate with B on top of A.

When a UIButton on B is tapped B slides off to the left with the following code:

[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];

On the right edge I want a 20px drop shadow but i cannot use the shadow properties of BView.view.layer.shadow.... because its 3.2/4.0+ only. And the performance of the animation is awful, the slide is lagging extremely much (Its fluent without the shadow).

I'm thinking using a custom UIView and do some magic in drawRect but is this possible or can i only draw within the bounds of the view?

Hope some of you guys and girls can help me.

Cheers objneodude


Solution

  • Solved with the following code

    // 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];