Created
December 13, 2014 18:48
-
-
Save Katafalkas/366a9f62444e4158c061 to your computer and use it in GitHub Desktop.
Swift Drawing Bubble Manually. Dynamiv Bubble View class
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
import UIKit | |
class MyView1 : UIView { | |
override func drawRect(rect: CGRect) { | |
var bubbleColor = UIColor.whiteColor() | |
bubbleColor.setStroke() | |
bubbleColor.setFill() | |
var bubbleSpace = CGRectMake(16.0, self.bounds.origin.y + 2.0, self.bounds.width - 18.0, self.bounds.height - 4.0) | |
let bubblePath = UIBezierPath(roundedRect: bubbleSpace, cornerRadius: 16.0) | |
bubblePath.stroke() | |
bubblePath.fill() | |
var triangleSpace = CGRectMake(0.0, self.bounds.height - 16.0, 16.0, 16.0) | |
var trianglePath = UIBezierPath() | |
var startPoint = CGPoint(x: 32.0, y: self.bounds.height - 16.0) | |
var tipPoint = CGPoint(x: 8.0, y: self.bounds.height - 8.0) | |
var endPoint = CGPoint(x: 32.0, y: self.bounds.height - 24.0) | |
trianglePath.moveToPoint(startPoint) | |
trianglePath.addLineToPoint(tipPoint) | |
trianglePath.addLineToPoint(endPoint) | |
trianglePath.closePath() | |
trianglePath.stroke() | |
trianglePath.fill() | |
} | |
} | |
var bar = CGFloat((3 * M_PI) / 2) | |
println(bar) | |
let kDeviceWidth = CGFloat(320.0) | |
class MyView2 : UIView { | |
var width:CGFloat! | |
var alignment:String! | |
var color:UIColor! | |
var border:CGFloat = CGFloat(2.0) | |
var cornerRadius = CGFloat(16.0) | |
override func drawRect(rect: CGRect) { | |
super.drawRect(rect) | |
enum Angle { | |
case TopLeft | |
case TopRight | |
case BottomLeft | |
case BottomRight | |
var start: CGFloat { | |
switch self { | |
case .TopLeft: | |
return CGFloat(M_PI) | |
case .TopRight: | |
return CGFloat(3 * M_PI_2) | |
case .BottomLeft: | |
return CGFloat(M_PI_2) | |
case .BottomRight: | |
return CGFloat(0) | |
} | |
} | |
var end: CGFloat { | |
switch self { | |
case .TopLeft: | |
return CGFloat(3 * M_PI_2) | |
case .TopRight: | |
return CGFloat(0) | |
case .BottomLeft: | |
return CGFloat(M_PI) | |
case .BottomRight: | |
return CGFloat(M_PI_2) | |
} | |
} | |
} | |
// enum Alignment { | |
//// static let border = self.border | |
//// static let bounds = self.bounds | |
//// static let width = self.width | |
// | |
// case Right() | |
// case Left | |
// case Center | |
// | |
// var leftBorder: CGFloat { | |
// switch self { | |
// case .Right: | |
// return Alignment.bounds.width - Alignment.border - Alignment.width | |
// case .Left: | |
// return Alignment.border + Alignment.width | |
// case .Center: | |
// return Alignment.border + CGFloat((Alignment.bounds.width - Alignment.width - (2*Alignment.border)) / 2) | |
// } | |
// } | |
// | |
// var rightBorder: CGFloat { | |
// switch self { | |
// case .Right: | |
// return Alignment.border | |
// case .Left: | |
// return Alignment.border | |
// case .Center: | |
// return Alignment.border + CGFloat((Alignment.bounds.width - Alignment.width - (2*Alignment.border)) / 2) | |
// } | |
// } | |
// } | |
var bubbleColor = self.color | |
bubbleColor.setStroke() | |
bubbleColor.setFill() | |
// var border = CGFloat(2.0) | |
var bubbleWidth = self.width | |
var leftBorder = self.border | |
var rightBorder = self.border | |
var bubbleAlignment = self.alignment | |
switch bubbleAlignment { | |
case "Right": | |
rightBorder = border + CGFloat(0.0) | |
leftBorder = self.bounds.width - rightBorder - bubbleWidth | |
// rightBorder = Alignment.Right.rightBorder | |
// leftBorder = Alignment.Right.leftBorder | |
case "Left": | |
leftBorder = border + CGFloat(0.0) | |
rightBorder = leftBorder + bubbleWidth | |
// rightBorder = Alignment.Left.rightBorder | |
// leftBorder = Alignment.Left.leftBorder | |
default: | |
var leftRightBorder = (self.bounds.width - bubbleWidth - (2*border)) / 2 | |
leftBorder = border + CGFloat(leftRightBorder) | |
rightBorder = border + CGFloat(leftRightBorder) | |
// rightBorder = Alignment.Center.rightBorder | |
// leftBorder = Alignment.Center.leftBorder | |
} | |
if (bubbleWidth + leftBorder + rightBorder) > self.bounds.width { | |
println("SOUND THE ALARM ! TOTAL width is too wide") | |
} | |
var topBottomBorder = border | |
var cornerRadius = self.cornerRadius | |
println("lB: \(leftBorder), W: \(bubbleWidth), R: \(cornerRadius), rB: \(rightBorder)") | |
var bubble = UIBezierPath() | |
bubble.moveToPoint(CGPoint( | |
x: leftBorder + cornerRadius, | |
y: topBottomBorder | |
)) | |
bubble.addLineToPoint(CGPoint( | |
x: leftBorder + bubbleWidth - cornerRadius, | |
y: topBottomBorder | |
)) | |
var topRightArcCenter = CGPoint( | |
x: leftBorder + bubbleWidth - cornerRadius, | |
y: topBottomBorder + cornerRadius | |
) | |
bubble.addArcWithCenter(topRightArcCenter, radius: cornerRadius, startAngle: Angle.TopRight.start, endAngle: Angle.TopRight.end, clockwise: true) | |
bubble.addLineToPoint(CGPoint( | |
x: leftBorder + bubbleWidth, | |
y: self.bounds.height - cornerRadius - topBottomBorder | |
)) | |
var botRightArcCenter = CGPoint( | |
x: leftBorder + bubbleWidth - cornerRadius, | |
y: self.bounds.height - cornerRadius - topBottomBorder | |
) | |
bubble.addArcWithCenter(botRightArcCenter, radius: cornerRadius, startAngle: Angle.BottomRight.start, endAngle: Angle.BottomRight.end, clockwise: true) | |
bubble.addLineToPoint(CGPoint( | |
x: leftBorder + cornerRadius, | |
y: self.bounds.height - topBottomBorder | |
)) | |
var botLeftArcCenter = CGPoint( | |
x: leftBorder + cornerRadius, | |
y: self.bounds.height - cornerRadius - topBottomBorder | |
) | |
bubble.addArcWithCenter(botLeftArcCenter, radius: cornerRadius, startAngle: Angle.BottomLeft.start, endAngle: Angle.BottomLeft.end, clockwise: true) | |
bubble.addLineToPoint(CGPoint( | |
x: leftBorder, | |
y: topBottomBorder + cornerRadius | |
)) | |
var topLeftArcCenter = CGPoint( | |
x: leftBorder + cornerRadius, | |
y: topBottomBorder + cornerRadius | |
) | |
bubble.addArcWithCenter(topLeftArcCenter, radius: cornerRadius, startAngle: Angle.TopLeft.start, endAngle: Angle.TopLeft.end, clockwise: true) | |
bubble.closePath() | |
bubble.fill() | |
bubble.stroke() | |
} | |
init(bubbleWidth width: CGFloat, bubbleAlignment alignment: String, bubbleColor color: UIColor, frame: CGRect) { | |
super.init(frame: frame) | |
self.width = width | |
self.alignment = alignment | |
self.color = color | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} | |
var view = MyView2(bubbleWidth: CGFloat(160), bubbleAlignment: "Right", bubbleColor:UIColor.whiteColor(), frame: CGRectMake(0.0, 0.0, kDeviceWidth, 200.0)) | |
//view.bubbleWidth = CGFloat(160) | |
view.backgroundColor = UIColor.blueColor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment