Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private func setUp(){
- let path = roundedPolygonWithRect(frame, lineWidth: 4, sides: 6, cornerRadius: 15 )
- maskLayer.path = path.CGPath
- maskLayer.lineWidth = CGFloat(lineWidth)
- maskLayer.strokeColor = UIColor.clearColor().CGColor
- maskLayer.fillColor = UIColor.whiteColor().CGColor
- self.layer.mask = maskLayer
- borderLayer.path = path.CGPath
- borderLayer.lineWidth = CGFloat(lineWidth)
- borderLayer.strokeColor = UIColor(red: 188, green: 218, blue: 212, alpha: 1.0).CGColor
- borderLayer.fillColor = UIColor.clearColor().CGColor
- self.layer.addSublayer(borderLayer)
- }
- private func updatePaths(){
- let path = roundedPolygonWithRect(frame, lineWidth: lineWidth, sides: numberOfSides, cornerRadius: cornerRadius )
- maskLayer.path = path.CGPath
- borderLayer.path = path.CGPath
- }
- /** Create UIBezierPath for regular polygon with rounded corners
- *
- * @param square The CGRect of the square in which the path should be created.
- * @param lineWidth The width of the stroke around the polygon. The polygon will be inset such that the stroke stays within the above square.
- * @param sides How many sides to the polygon (e.g. 6=hexagon; 8=octagon, etc.).
- * @param cornerRadius The radius to be applied when rounding the corners.
- *
- * @return UIBezierPath of the resulting rounded polygon path.
- */
- private func roundedPolygonWithRect(square:CGRect, lineWidth:Float, sides:Int, cornerRadius:Float) -> UIBezierPath {
- var path = UIBezierPath()
- // how much to turn at every corner
- let theta = Float(2.0 * M_PI) / Float(sides)
- // offset from which to start rounding corners
- let offset:Float = cornerRadius * tanf( theta / 2.0 )
- let squareWidth = Float( min(square.size.width, square.size.height) )
- var length = squareWidth - lineWidth
- if( sides % 4 != 0){
- length = length * cosf(theta / 2.0) // + offset / 2.0
- }
- let sideLength = length * Float(tanf(theta / 2.0))
- // start drawing ap 'point' in lower right corner
- let p1 = squareWidth / 2.0 + sideLength / 2.0 - offset
- let p2 = squareWidth - ((squareWidth - length) / 2.0)
- println("P1 = (p1)")
- println("P2 = (p2)")
- var point = CGPointMake(CGFloat(p1), CGFloat(p2))
- var angle = Float(M_PI)
- path.moveToPoint(point)
- // draw the sides around rounded corners of the polygon
- for( var i = 0; i < sides; i++){
- let x1 = Float(point.x) + ((sideLength - offset * 2.0) * cosf(angle))
- let y1 = Float(point.y) + ((sideLength - offset * 2.0) * sinf(angle))
- println("X = (x1)")
- println("Y = (y1)")
- point = CGPointMake(CGFloat(x1), CGFloat(y1))
- path.addLineToPoint(point)
- let centerX = Float(point.x) + (cornerRadius) * cosf(angle + Float(M_PI_2))
- let centerY = Float(point.y) + (cornerRadius) * sinf(angle + Float(M_PI_2))
- let center = CGPointMake(CGFloat(centerX), CGFloat(centerY))
- let radius:CGFloat = CGFloat(cornerRadius)
- let startAngle = CGFloat(angle) - CGFloat(M_PI_2)
- let endAngle = CGFloat(angle) + CGFloat(theta) - CGFloat(M_PI_2)
- path.addArcWithCenter(center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
- point = path.currentPoint
- angle += theta
- }
- path.closePath()
- return path
- }
- var angle = Float(M_PI_2)
- let p1 = (squareWidth / 2.0 + sideLength / 2.0 - offset)+50
- let p2 = squareWidth - ((squareWidth - length) / 2.0)-120
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement