Assign toValue from an int - xcode

I'm doing an animation and want the toValue to get bigger every time i tap an image. My code will work (but doesn't now, of course), but how do you make a toValue an integer?
I have this:
int comboTapAnim = (comboTap / 10) + 1;
then in the animation:
theAnimation.toValue = [comboTapAnim];
Xcode no likey. I have tried other combinations but it must be an NSNumber but I want it to be an int.

I found out the answer!!
[NSNumber numberWithInt:<#(int)#>]

Related

NSMutableArray of NSNumber is nil after adding parsed float

I am trying to parse an array of Vectors (from C++'s std::vector) to an NSMutableArray of NSNumber. However, the Vector that I add to the NSNumber array is always nil, even after adding a NSNumber (parsed from std::vector)
In this project, I'm trying to connect the OpenCV Canny Edge Detection Algorithm (C++) to my Swift project. One of their methods returns all the photo's edges as std::vector. So, I'm trying to parse this into an array that is compatible with Swift (using Objective-C++ as the intermediary language). As of right now, it seems that only one line isn't working correctly (as stated above).
I am also a beginner Objective-C++ programmer, so there may be some common errors in the code, which I haven't found.
/*Ptr<LineSegmentDetector>*/
cv::Ptr<cv::LineSegmentDetector> ls = cv::createLineSegmentDetector(cv::LSD_REFINE_STD) ;//Creates the line detector (AKA what I called the edge detector)
std::vector<cv::Vec4f> lines_std;
[image convertToMat: &mat];
// Detect the lines
ls->detect(mat, lines_std);
//Swift compatible NSMutableArray to return from this method
NSMutableArray *edgeLines;
printf("currently finding the edge lines");
for (int i = 0; i < lines_std.size(); i ++) {
NSMutableArray<NSNumber *> *vector;
//Convert from Vec4f to NSNumber
for (int k = 0; k < 4; k ++) {
[vector addObject: [NSNumber numberWithFloat: (lines_std[i][k])]]; //HERE, I'm parsing each float value in each vector (from std::vector<cv::Vec4f>) and adding it to a NSArray, like a vector
}
//Here, I add the vectors to the return NSMutable array
[edgeLines insertObject:vector atIndex:edgeLines.count];
}
return edgeLines;
As stated above, the "vector" variable is always nil, even though I'm adding an NSNumber object to it.
I found the problem, I hadn't used this line:
NSMutableArray *vector = [[NSMutableArray alloc] init];

How to convert dp to px in xamarin.android?

I want to convert dp to px in my C# code in xamarin.android, but all I could find were java codes in android studio that have some problems in xamarin. I tried to use equivalent like using Resources instead of getResources() and I could solve some little problems, but there are some problems yet that I couldn't find any equivalent for them. here are original codes, my codes, and my codes problems in xamarin:
First code
(found from Programatically set height on LayoutParams as density-independent pixels)
java code
int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, < HEIGHT >, getResources().getDisplayMetrics());
C# code
int height = (int)TypedValue.ApplyDimension(TypedValue.COMPLEX_UNIT_DIP, < HEIGHT >, Resources.DisplayMetrics);
problems:
'TypedValue' does not contain a definition for 'COMPLEX_UNIT_DIP'
Invalid expression term < (The same error for >)
The name 'HEIGHT' does not exist in the current context
Second code
(found from Formula px to dp, dp to px android)
java code
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
C# code
DisplayMetrics displayMetrics = Application.Context.Resources.DisplayMetrics;
int pixel = Math.Round(dp * (displayMetrics.Xdpi / DisplayMetrics.DensityDefault));
problem
Operator '/' cannot be applied to operands of type 'float' and 'DisplayMetricsDensity'
Now I have actually two questions. Which code is more proper? What's equivalent code for them in xamarin.android?
Thanks in advance.
Solution to "First code":
Xamarin tends to move constants into their own enums. COMPLEX_UNIT_DIP can be found on ComplexUnitType enum. Also you cannot have < HEIGHT > in your code you actually need to pass in dips to get the equivalent pixel value. in the example below I am getting pixels in 100 dips.
var dp = 100;
int pixel = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, Context.Resources.DisplayMetrics);
Solution to "Second code":
You need to explicitly cast 'DisplayMetrics.DensityDefault' to a float and the entire round to an int:
int pixel = (int)System.Math.Round(dp * (displayMetrics.Xdpi / (float)DisplayMetrics.DensityDefault));
I prefer the first approach as the second code is specifically for calculating along the "x dimension":
Per Android docs and Xamarin.Android docs, the Xdpi property is
"The exact physical pixels per inch of the screen in the X dimension."
These are from the project that I am currently working on..
public static float pxFromDp(Context context, float dp)
{
return dp * context.Resources.DisplayMetrics.Density;
}
public static float dpFromPx(Context context, float px)
{
return px / context.Resources.DisplayMetrics.Density;
}
From android source code textview settextsize
var convertToDp = TypedValue.ApplyDimension( ComplexUnitType.Dip, size, context.Resouces.DisplayMetrics);
dp to px:
DisplayMetrics displayMetrics = Application.Context.Resources.DisplayMetrics;
double pixel = Math.Round((dp * DisplayMetrics.DensityDefault) + 0.5);
Answer taken from here : https://stackoverflow.com/a/8490361/6949388
Sorry, not enough rep to comment.

Changing size of NSTextView

I am trying to implement an NSTextView which resizes to fit its content. I am trying to make it so it has a maximum amount of lines. Meaning, that when this limit is reached, it will not grow any further and the user will be able to scroll it.
There seem to be a lot of people wanting this but I have not found a complete implementation of this.
The idea is to take the content size of the NSTextView and resize it so that it matches. I just can't figure out how to set the layout size of the NSTextView. It does not seem to be it's frame which needs to be set and which one might expect.
Can anyone please tell me how to set the frame of the NSTextView?
It turns out that it's the height of the scroll view which should be changed. Changing the height of an NSTextView can be done by overwriting the -didChangeText method like below.
- (void)didChangeText
{
NSScrollView *scrollView = (NSScrollView *) self.superview.superview;
NSRect frame = scrollView.frame;
// Calculate height
NSUInteger numberOfLines = [self numberOfLines];
NSUInteger height = frame.size.height;
if (numberOfLines <= 13)
{
height = 22 + (numberOfLines - 1) * 14;
if (height < 22)
height = 22;
}
// Update height
frame.size.height = height;
self.superview.superview.frame = frame;
}
Play around with the constants to get the result you wish for.

Bars not aligned with X-Axis ticks in CPTBarPlot of Core Plot

I have a problem with CPTBarPlot in Core Plot on Cocoa.
Namely, I cannot achieve to control step width of bars. X-Axis ticks are set normally as integer from 1 to infinite and drawing Scatter Plot on it works.
However, when bar plot is drawn, only first columns starts (with proper offset, of course) on first tick. Second is a bit further form its tick, so is third and all others as shown on screenshot:
I haven't found any property to control this, however, I found that data source method
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
Behaves differently than when I draw Scatter Plot. namely, it enumerates only integer 3, which is CPTBarPlotBindingBarTips and doesn't enumerate any other constant, I am especially bothered with absence of CPTBarPlotBindingBarLocations.
If I start VerticalBarChart from Plot Gallery mac demo project I've checked and seen that same method enumerates 3,4 and 2 constants in same order.
I am using Core Plot 0.9 namely, I believe the same as in this demo. Still...
How to solve this?
Here is the complete code for labels and ticks on X axis;
x.labelRotation = M_PI/4;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelOffset = 0.0;
CPTMutableTextStyle *labelStyle = [CPTMutableTextStyle textStyle];
labelStyle.fontSize = 9.0;
x.labelTextStyle = labelStyle;
// NSMutableArray *dateArray = [dataSeries objectAtIndex:0];
NSMutableArray *customTickLocations = [NSMutableArray array];
NSMutableArray *xAxisLabels = [NSMutableArray array];
for (int i=0;i<[dataSeries count];i++) {
[customTickLocations addObject:[NSDecimalNumber numberWithInt:i+1]];
[xAxisLabels addObject:[dataSeries objectAtIndex:i]];
}
// [customTickLocations addObject:[NSDecimalNumber numberWithInt:[dateArray count]]];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
newLabel.rotation = M_PI/4;
[customLabels addObject:newLabel];
[newLabel release];
}
[x setMajorTickLocations:[NSSet setWithArray:customTickLocations]];
x.axisLabels = [NSSet setWithArray:customLabels];
I have to add that same ticks are being used when scatter plot is drawn on same plot space and it matches perfectly....
The field parameter will be one of the members of this enum:
typedef enum _CPTBarPlotField {
CPTBarPlotFieldBarLocation = 2, ///< Bar location on independent coordinate axis.
CPTBarPlotFieldBarTip = 3, ///< Bar tip value.
CPTBarPlotFieldBarBase = 4 ///< Bar base (used only if barBasesVary is YES).
} CPTBarPlotField;
If the plot's plotRange property is nil (the default), the plot will ask the datasource for locations and tips for each bar. If barBasesVary == YES, it will also ask for base values for each bar.

Is there any method in Cocoa that helps iterating through all NSColor values(R,G,B only, alpha not touched)?

I need a function that for a given R,G,B values returns the next color.
By saying the next color I mean the color which one of the components change by 1(assuming that R,G,B values are of range 0-255).
It has to be constructed that way that starting from the white color(or black alternatively) you can iterate throught all of the colors(which count is 255*255*255). It would be nice, but not required that after iterating throught all of the colors, they are being repeated from the beginning.
You could base color retrieval on color lists, either using a custom or Apple one like "Web Safe Colors":
NSColorList* cl = [NSColorList colorListNamed:#"Web Safe Colors"];
NSLog(#"%#", cl);
for (NSString *key in [cl allKeys]) {
NSLog(#" * %# - %#", key, [cl colorWithKey:key]);
}
If you need more and the order doesn't matter, you could use e.g. +colorWithColorSpace:components:count::
uint32_t c = 0;
// increment c whenever a color is requested ...
CGFloat components[4] = {
((c & 0xFF0000) >> 16) / 256.0,
((c & 0x00FF00) >> 8 ) / 256.0,
((c & 0x0000FF) >> 0 ) / 256.0,
0.0
};
NSColor *color = [NSColor colorWithColorSpace:[NSColorSpace genericRGBColorSpace]
components:components
count:4];
It is not clear what you mean with "next color" but you can easily have a counter from which you extract the components:
uint32 color = 0;
uint8 r = color%256;
uint8 g = (color/256) % 256;
uint8 b = (color/(256*256)) % 256;
// use color
++color;
otherwise you can just generate random colors and keep track of already used ones with an NSSet. Of course you are gonna pack 3 components in a single object, like an int.

Resources