BEMSimpleLineGraph
BEMSimpleLineGraph copied to clipboard
The bottomGradient property broken in Swift
I don't know why. but if I set bottomGradient in swift will crash at BEMLine.m line 200 CGContextDrawLinearGradient(ctx, self.bottomGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillBottom.bounds)), 0);
First I use var components: [CGFloat] = [1, 1, 1, 1, 1, 1, 1, 0] then use some method transfer array to UnsafePointer<CGFloat> Not work at all
The same issue. Please, help to fix
Had the same issue today. My workaround was bridging that part to Objective-C.
#import <Foundation/Foundation.h>
#import "BEMSimpleLineGraphView.h"
@interface MEFBridgeGradient : NSObject
+ (void)setGradientForGraph:(BEMSimpleLineGraphView *)graph;
@end
#import "MEFBridgeGradient.h"
@implementation MEFBridgeGradient
+ (void)setGradientForGraph:(BEMSimpleLineGraphView *)graph {
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = {
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 0.0
};
graph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
}
@end
And just call that in swift:
MEFBridgeGradient.setGradientForGraph(yourLineGraphView)
Don't forget to add the class to your *-Bridging-Header.h.
Hope that helps.
Or you can simply create the global variable for the CGGradient color.
class xxx {
var gradient : CGGradient?
override func viewDidLoad{
let topColor = graph.colorBottom
let bottomColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.5)
let gradientColors : [CGColor] = [topColor.CGColor,bottomColor.CGColor]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let locations : [CGFloat] = [0.0,1.0]
gradient = CGGradientCreateWithColors(colorSpace, gradientColors, locations)
graph.gradientBottom = gradient
}
}
This works just fine
@beheim Thanks! It's really work! 👍🏼