RubyMotion and pointers

I am new to Objective-C, searched high and low without finding an answer to this:

In my RubyMotion project, I have a subclass of UIView called StatusGuage that contains the drawLinearGradient method as follows:

def drawLinearGradient(context, rect, startColor, endColor) colorspace = CGColorSpaceCreateDeviceRGB() locations = [0.0, 1.0] # colors = NSArray.arrayWithObjects(startColor, endColor, nil) # ptrColors = Pointer.new(:object, colors) colors = [startColor, endColor, nil] # CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) colors, locations); CGGradientCreateWithColors(colorspace, colors, locations) end 

I would like to know how to call CGGradientCreateWithColors. It explicitly expects a pointer (CFArrayRef), but I cannot figure out how to pass this. One of the iterations I tried is commented out.

Here is the error message:

 2012-05-11 16:57:36.331 HughesNetMeter[34906:17903] *** Terminating app due to uncaught exception 'TypeError', reason: 'status_guage.rb:43:in `drawLinearGradient:': expected instance of Pointer, got `[0.0, 1.0]' (Array) (TypeError) from status_guage.rb:13:in `drawRect:' 

Thanks for any help.

+6
source share
1 answer

Few things. The error does not talk about colors, this refers to the const CGFloat locations[] argument of const CGFloat locations[] .

It must be a pointer, which can be achieved as follows ( Link to a pointer class )

 locations = Pointer.new(:float, 2) locations[1] = 1.0 

Next, your array does not need to complete nil . In Ruby, this will create an array with three objects, which is not what you want, since it will most likely call the CGGradientCreateWithColors() function to mess up


This looks like an example of http://www.raywenderlich.com/ , so the rest of it

 def drawLinearGradient(context, rect, startColor, endColor) colorspace = CGColorSpaceCreateDeviceRGB() locations = Pointer.new(:float, 2) locations[1] = 1.0 colors = [startColor, endColor] gradient = CGGradientCreateWithColors(colorspace, colors, locations) startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)) endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)) CGContextSaveGState(context) CGContextAddRect(context, rect) CGContextClip(context) CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0) CGContextRestoreGState(context) end 

Final note

The locations argument is not required even in this case, since CGGradientCreateWithColors() will automatically set the values ​​0.0 and 1.0 for the first and last color. Check out the CGGradient Reference

Places
Location for each color provided by the components. Each location must be a CGFloat value between 0 and 1 inclusive. If 0 and 1 are not in the array of locations, Quartz uses colors that are closest to 0 and 1 for these locations.
If the locations are NULL, the first color in the color is assigned to location 0, the last color colors are assigned to location 1, and the intermediate colors are assigned to places located between equal intervals.

+9
source

Source: https://habr.com/ru/post/915541/


All Articles