BEMSimpleLineGraph icon indicating copy to clipboard operation
BEMSimpleLineGraph copied to clipboard

The bottomGradient property broken in Swift

Open nyssance opened this issue 11 years ago • 4 comments

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

nyssance avatar Nov 21 '14 14:11 nyssance

The same issue. Please, help to fix

kasnitski avatar Jan 08 '15 19:01 kasnitski

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.

beheim avatar Jan 30 '15 23:01 beheim

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

Rameshv avatar Mar 29 '15 12:03 Rameshv

@beheim Thanks! It's really work! 👍🏼

InstaRobot avatar May 29 '16 04:05 InstaRobot