Wednesday, February 15, 2017

Shadow on UIView using UIBezierPath


//Shadow on the right side
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(view.width - 1, -4, 2, view.height)];
view.layer.masksToBounds = NO;
view.layer.shadowColor   = [UIColor blackColor].CGColor;
view.layer.shadowOffset  = CGSizeMake(0.0f, 5.0f);
view.layer.shadowOpacity = 0.4f;
view.layer.shadowPath    = shadowPath.CGPath;

view.layer.shadowRadius  = 2.0f;

//Shadow on the bottom side
let shadowPath:UIBezierPath   = UIBezierPath.init(rect:CGRect(x: -4y: self.height - 1width : self.width,height: 1))
self.layer.masksToBounds      = false
self.layer.shadowColor        = UIColor.black.cgColor
self.layer.shadowOffset       = CGSize(width : 0.0, height :1.0)
self.layer.shadowOpacity      = 0.4
self.layer.shadowPath         = shadowPath.cgPath
self.layer.shadowRadius       = 2.0

[There are lot of customisation options for shadows using UIBezierPath... 

Check links below: 

https://nachbaur.com/2010/11/16/fun-shadow-effects-using-custom-calayer-shadowpaths/

https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/



Swift 3.0 version : 


        //Shadow on the bottom side
        let shadowPath:UIBezierPath   = UIBezierPath.init(rect:CGRect(x: -4,
                                                                      y: self.height - 1,
                                                                 width : self.width,
                                                                 height: 2))
        self.layer.masksToBounds      = false
        self.layer.shadowColor        = UIColor.black.cgColor
        self.layer.shadowOffset       = CGSize(width : 0.0, height :5.0)
        self.layer.shadowOpacity      = 0.4
        self.layer.shadowPath         = shadowPath.cgPath
        

        self.layer.shadowRadius       = 2.0

No comments:

Post a Comment