|
| 1 | +// |
| 2 | +// KtCorner.swift |
| 3 | +// CornerRadius |
| 4 | +// |
| 5 | +// Created by 张星宇 on 16/2/28. |
| 6 | +// Copyright © 2016年 zxy. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import UIKit |
| 11 | + |
| 12 | +private func roundbyunit(num: Double, inout _ unit: Double) -> Double { |
| 13 | + let remain = modf(num, &unit) |
| 14 | + if (remain > unit / 2.0) { |
| 15 | + return ceilbyunit(num, &unit) |
| 16 | + } else { |
| 17 | + return floorbyunit(num, &unit) |
| 18 | + } |
| 19 | +} |
| 20 | +private func ceilbyunit(num: Double, inout _ unit: Double) -> Double { |
| 21 | + return num - modf(num, &unit) + unit |
| 22 | +} |
| 23 | + |
| 24 | +private func floorbyunit(num: Double, inout _ unit: Double) -> Double { |
| 25 | + return num - modf(num, &unit) |
| 26 | +} |
| 27 | + |
| 28 | +private func pixel(num: Double) -> Double { |
| 29 | + var unit: Double |
| 30 | + switch Int(UIScreen.mainScreen().scale) { |
| 31 | + case 1: unit = 1.0 / 1.0 |
| 32 | + case 2: unit = 1.0 / 2.0 |
| 33 | + case 3: unit = 1.0 / 3.0 |
| 34 | + default: unit = 0.0 |
| 35 | + } |
| 36 | + return roundbyunit(num, &unit) |
| 37 | +} |
| 38 | + |
| 39 | +extension UIView { |
| 40 | + func kt_addCorner(radius radius: CGFloat) { |
| 41 | + self.kt_addCorner(radius: radius, borderWidth: 1, backgroundColor: UIColor.clearColor(), borderColor: UIColor.blackColor()) |
| 42 | + } |
| 43 | + |
| 44 | + func kt_addCorner(radius radius: CGFloat, |
| 45 | + borderWidth: CGFloat, |
| 46 | + backgroundColor: UIColor, |
| 47 | + borderColor: UIColor) { |
| 48 | + let imageView = UIImageView(image: kt_drawRectWithRoundedCorner(radius: radius, |
| 49 | + borderWidth: borderWidth, |
| 50 | + backgroundColor: backgroundColor, |
| 51 | + borderColor: borderColor)) |
| 52 | + self.insertSubview(imageView, atIndex: 0) |
| 53 | + } |
| 54 | + |
| 55 | + func kt_drawRectWithRoundedCorner(radius radius: CGFloat, |
| 56 | + borderWidth: CGFloat, |
| 57 | + backgroundColor: UIColor, |
| 58 | + borderColor: UIColor) -> UIImage { |
| 59 | + let sizeToFit = CGSize(width: pixel(Double(self.bounds.size.width)), height: Double(self.bounds.size.height)) |
| 60 | + let halfBorderWidth = CGFloat(borderWidth / 2.0); |
| 61 | + |
| 62 | + UIGraphicsBeginImageContextWithOptions(sizeToFit, false, UIScreen.mainScreen().scale) |
| 63 | + let context = UIGraphicsGetCurrentContext() |
| 64 | + |
| 65 | + CGContextSetLineWidth(context, borderWidth); |
| 66 | + CGContextSetStrokeColorWithColor(context, borderColor.CGColor); |
| 67 | + CGContextSetFillColorWithColor(context, backgroundColor.CGColor); |
| 68 | + |
| 69 | + let width = sizeToFit.width, height = sizeToFit.height |
| 70 | + CGContextMoveToPoint(context, width - halfBorderWidth, radius + halfBorderWidth); // 开始坐标右边开始 |
| 71 | + CGContextAddArcToPoint(context, width - halfBorderWidth, height - halfBorderWidth, width - radius - halfBorderWidth, height - halfBorderWidth, radius); // 右下角角度 |
| 72 | + CGContextAddArcToPoint(context, halfBorderWidth, height - halfBorderWidth, halfBorderWidth, height - radius - halfBorderWidth, radius); // 左下角角度 |
| 73 | + CGContextAddArcToPoint(context, halfBorderWidth, halfBorderWidth, width - halfBorderWidth, halfBorderWidth, radius); // 左上角 |
| 74 | + CGContextAddArcToPoint(context, width - halfBorderWidth, halfBorderWidth, width - halfBorderWidth, radius + halfBorderWidth, radius); // 右上角 |
| 75 | + |
| 76 | + CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke) |
| 77 | + let output = UIGraphicsGetImageFromCurrentImageContext(); |
| 78 | + UIGraphicsEndImageContext(); |
| 79 | + return output |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension UIImageView { |
| 84 | + /** |
| 85 | + / !!!只有当 imageView 不为nil 时,调用此方法才有效果 |
| 86 | + |
| 87 | + :param: radius 圆角半径 |
| 88 | + */ |
| 89 | + override func kt_addCorner(radius radius: CGFloat) { |
| 90 | + self.image = self.image?.kt_drawRectWithRoundedCorner(radius: radius, self.bounds.size) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +extension UIImage { |
| 95 | + func kt_drawRectWithRoundedCorner(radius radius: CGFloat, _ sizetoFit: CGSize) -> UIImage { |
| 96 | + let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: sizetoFit) |
| 97 | + |
| 98 | + UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.mainScreen().scale) |
| 99 | + CGContextAddPath(UIGraphicsGetCurrentContext(), |
| 100 | + UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.AllCorners, |
| 101 | + cornerRadii: CGSize(width: radius, height: radius)).CGPath) |
| 102 | + CGContextClip(UIGraphicsGetCurrentContext()) |
| 103 | + |
| 104 | + self.drawInRect(rect) |
| 105 | + CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke) |
| 106 | + let output = UIGraphicsGetImageFromCurrentImageContext(); |
| 107 | + UIGraphicsEndImageContext(); |
| 108 | + |
| 109 | + return output |
| 110 | + } |
| 111 | +} |
0 commit comments