Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active January 13, 2016 05:11
Show Gist options
  • Save shoheiyokoyama/804a2ee38a917312da9e to your computer and use it in GitHub Desktop.
Save shoheiyokoyama/804a2ee38a917312da9e to your computer and use it in GitHub Desktop.
透過性のあるUIViewサブクラスの影の描画と実装方法について ref: http://qiita.com/shoheiyokoyama/items/d7d02f16b4f632ba6584
shadowButton.layer.cornerRadius = 15;
shadowButton.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
shadowButton.backgroundColor = [UIColor clearColor];
CGFloat w = 6.0f;
CGFloat wh = w / 2;
CGFloat sw = shadowButton.frame.size.width;
CGFloat sh = shadowButton.frame.size.height;
CGFloat c = shadowButton.layer.cornerRadius;
CGMutablePathRef pathRef = CGPathCreateMutable();
//Start Point
CGPathMoveToPoint(pathRef , nil, -wh, -wh + c);
CGPathAddArcToPoint(pathRef , nil, -wh, -wh, -wh + c, -wh, c);
//Top Outside
CGPathAddLineToPoint(pathRef, nil, sw + wh - c, -wh);
CGPathAddArcToPoint(pathRef , nil, sw+wh, -wh, sw + wh, -wh + c, c);
CGPathAddLineToPoint(pathRef, nil, sw - wh, wh + c);
CGPathAddArcToPoint(pathRef , nil, sw - wh, wh, sw - wh - c, wh, c);
//Top Inside
CGPathAddLineToPoint(pathRef, nil, wh + c, wh);
CGPathAddArcToPoint(pathRef , nil, wh, wh, wh, wh + c, c);
//Left Inside
CGPathAddLineToPoint(pathRef, nil, wh, sh - wh - c);
CGPathAddArcToPoint(pathRef , nil, wh, sh - wh, wh + c, sh - wh, c);
//Bottom Inside
CGPathAddLineToPoint(pathRef, nil, sw - wh - c, sh - wh);
CGPathAddArcToPoint(pathRef , nil, sw - wh, sh - wh, sw - wh, sh - wh - c, c);
//Right Inside
CGPathAddLineToPoint(pathRef, nil, sw - wh, wh + c);
CGPathAddLineToPoint(pathRef, nil, sw + wh, -wh + c);
//Right Outside
CGPathAddLineToPoint(pathRef, nil, sw + wh, sh + wh - c);
CGPathAddArcToPoint(pathRef , nil, sw + wh, sh + wh, sw + wh - c, sh + wh, c);
//Bottom Outside
CGPathAddLineToPoint(pathRef, nil, -wh + c, sh + wh);
CGPathAddArcToPoint(pathRef , nil, -wh, sh + wh, -wh, sh + wh - c, c);
//Left Outside
CGPathAddLineToPoint(pathRef, nil, -wh, -wh + c);
CGPathCloseSubpath(pathRef);
shadowButton.layer.shadowPath = pathRef;
shadowButton.layer.cornerRadius = 15
shadowButton.layer.shadowOffset = CGSizeMake(0.0, 0.0)
shadowButton.backgroundColor = UIColor.clearColor()
let w: CGFloat = 6.0
let wh: CGFloat = w / 2
let sw = shadowButton.frame.width
let sh = shadowButton.frame.height
let c = shadowButton.layer.cornerRadius
let pathRef: CGMutablePathRef = CGPathCreateMutable()
//Start Point
CGPathMoveToPoint(pathRef , nil, -wh, -wh + c)
CGPathAddArcToPoint(pathRef , nil, -wh, -wh, -wh + c, -wh, c)
//Top Outside
CGPathAddLineToPoint(pathRef, nil, sw + wh - c, -wh)
CGPathAddArcToPoint(pathRef , nil, sw + wh, -wh, sw + wh, -wh+c, c)
CGPathAddLineToPoint(pathRef, nil, sw - wh, wh + c)
CGPathAddArcToPoint(pathRef , nil, sw - wh, wh, sw - wh - c, wh, c)
//Top Inside
CGPathAddLineToPoint(pathRef, nil, wh + c, wh)
CGPathAddArcToPoint(pathRef , nil, wh, wh, wh, wh + c, c)
//Left Inside
CGPathAddLineToPoint(pathRef, nil, wh, sh - wh - c)
CGPathAddArcToPoint(pathRef , nil, wh, sh - wh, wh + c, sh - wh, c)
//Bottom Inside
CGPathAddLineToPoint(pathRef, nil, sw - wh - c, sh - wh)
CGPathAddArcToPoint(pathRef , nil, sw - wh, sh - wh, sw - wh, sh - wh - c, c)
//Right Inside
CGPathAddLineToPoint(pathRef, nil, sw - wh, wh + c)
CGPathAddLineToPoint(pathRef, nil, sw + wh, -wh + c)
//Right Outside
CGPathAddLineToPoint(pathRef, nil, sw + wh, sh + wh - c)
CGPathAddArcToPoint(pathRef , nil, sw + wh, sh + wh, sw + wh - c, sh + wh, c)
//Bottom Outside
CGPathAddLineToPoint(pathRef, nil, -wh + c, sh + wh)
CGPathAddArcToPoint(pathRef , nil, -wh, sh + wh, -wh, sh + wh - c, c)
//Left Outside
CGPathAddLineToPoint(pathRef, nil, -wh, -wh + c)
CGPathCloseSubpath(pathRef)
shadowButton.layer.shadowPath = pathRef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment