开发者

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.开发者_JAVA技巧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


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


I had the same problem, after trying various ideas, I just added a subview a shadow colour (Gray) behind the UIView I needed to have a shadow, see code: (Included is the code for >3.2.x/4.x as well.

    /*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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜