Created
March 23, 2015 01:33
-
-
Save KelvinJin/ca9fce3fa77cb1233c2f to your computer and use it in GitHub Desktop.
Create your own rounded rect path with rounding corners.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RoundedCornerLayer.swift | |
// OmnyStudio | |
// | |
// Created by Jin Wang on 20/03/2015. | |
// Copyright (c) 2015 121cast. All rights reserved. | |
// | |
import UIKit | |
struct RoundedCornerType : RawOptionSetType { | |
typealias RawValue = UInt | |
private var value: UInt = 0 | |
init(_ value: UInt) { self.value = value } | |
init(rawValue value: UInt) { self.value = value } | |
init(nilLiteral: ()) { self.value = 0 } | |
static var allZeros: RoundedCornerType { return self(0) } | |
static func fromMask(raw: UInt) -> RoundedCornerType { return self(raw) } | |
var rawValue: UInt { return self.value } | |
static var None: RoundedCornerType { return self(0) } | |
static var UpperLeft: RoundedCornerType { return RoundedCornerType(1 << 0) } | |
static var UpperRight: RoundedCornerType { return RoundedCornerType(1 << 1) } | |
static var BottomLeft: RoundedCornerType { return RoundedCornerType(1 << 2) } | |
static var BottomRight: RoundedCornerType { return RoundedCornerType(1 << 3) } | |
} | |
class RoundedCornerLayer { | |
static func layerWithCorner(size: CGSize, radius: CGFloat, type: RoundedCornerType) -> CALayer { | |
//// Rectangle Drawing | |
var rectanglePath = UIBezierPath() | |
if (type & .UpperLeft) != nil { | |
rectanglePath.moveToPoint(CGPointMake(radius, 0)) | |
} else { | |
rectanglePath.moveToPoint(CGPointMake(0, 0)) | |
} | |
if (type & .UpperRight) != nil { | |
rectanglePath.addLineToPoint(CGPointMake(size.width - radius, 0)) | |
let center = CGPoint(x: size.width - radius, y: radius) | |
rectanglePath.addArcWithCenter(center, radius: radius, startAngle: 3 * CGFloat(M_PI_2), endAngle: 0, clockwise: true) | |
} else { | |
rectanglePath.addLineToPoint(CGPointMake(size.width, 0)) | |
} | |
if (type & .BottomRight) != nil { | |
rectanglePath.addLineToPoint(CGPointMake(size.width, size.height - radius)) | |
let center = CGPoint(x: size.width - radius, y: size.height - radius) | |
rectanglePath.addArcWithCenter(center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true) | |
} else { | |
rectanglePath.addLineToPoint(CGPointMake(size.width, size.height)) | |
} | |
if (type & .BottomLeft) != nil { | |
rectanglePath.addLineToPoint(CGPointMake(radius, size.height)) | |
let center = CGPoint(x: radius, y: size.height - radius) | |
rectanglePath.addArcWithCenter(center, radius: radius, startAngle: CGFloat(M_PI_2), endAngle: CGFloat(M_PI), clockwise: true) | |
} else { | |
rectanglePath.addLineToPoint(CGPointMake(0, size.height)) | |
} | |
if (type & .UpperLeft) != nil { | |
rectanglePath.addLineToPoint(CGPointMake(0, radius)) | |
let center = CGPoint(x: radius, y: radius) | |
rectanglePath.addArcWithCenter(center, radius: radius, startAngle: CGFloat(M_PI), endAngle: 3 * CGFloat(M_PI_2), clockwise: true) | |
} else { | |
rectanglePath.addLineToPoint(CGPointMake(0, 0)) | |
} | |
rectanglePath.closePath() | |
let layer = CAShapeLayer() | |
layer.path = rectanglePath.CGPath | |
return layer | |
} | |
} | |
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) | |
let centerView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) | |
centerView.center = myView.center | |
myView.addSubview(centerView) | |
let myLayer = RoundedCornerLayer.layerWithCorner(centerView.bounds.size, radius: 10, type: RoundedCornerType.UpperRight | RoundedCornerType.BottomRight) | |
centerView.backgroundColor = UIColor.redColor() | |
centerView.layer.mask = myLayer | |
// Output | |
myView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment