Created
May 31, 2016 08:39
-
-
Save InstaRobot/f59eaaaf2432e6a269f1e55eb562979d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Создаем пустой swift файл и вставляем в него код | |
| import UIKit | |
| @IBDesignable class GraphView: UIView { | |
| //1 - the properties for the gradient | |
| @IBInspectable var startColor: UIColor = UIColor.redColor() | |
| @IBInspectable var endColor: UIColor = UIColor.greenColor() | |
| override func drawRect(rect: CGRect) { | |
| //2 - get the current context | |
| let context = UIGraphicsGetCurrentContext() | |
| let colors = [startColor.CGColor, endColor.CGColor] | |
| //3 - set up the color space | |
| let colorSpace = CGColorSpaceCreateDeviceRGB() | |
| //4 - set up the color stops | |
| let colorLocations:[CGFloat] = [0.0, 1.0] | |
| //5 - create the gradient | |
| let gradient = CGGradientCreateWithColors(colorSpace, | |
| colors, | |
| colorLocations) | |
| //6 - draw the gradient | |
| var startPoint = CGPoint.zeroPoint | |
| var endPoint = CGPoint(x:0, y:self.bounds.height) | |
| CGContextDrawLinearGradient(context, | |
| gradient, | |
| startPoint, | |
| endPoint, | |
| 0) | |
| } | |
| } // @IBDesignable class GraphView | |
| // Добавляем на Storyboard UIView, и в Identity Inspector в поле Class выбираем GraphView. | |
| // Переходим в Attributes Inspector и выбираем нужные нам цвета. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment