CGPoint giving the same value for x and y? - cgpoint

This is a strange bug I'm experiencing when testing on an old iPod. I'm trying to get location values on touchesMoved, but the y always outputs value equal to x. If I log using NSStringFromCGPoint, then I get the correct output. How come accessing x and y separately is not working?
CGPoint location = [singleTouch locationInView:self.view];
NSLog(#"Location using NSStringFromCGPoint %#", NSStringFromCGPoint(location)); // output: {27, 136}
NSLog(#"Location using x and y %f %f",location.x, location.y); // output: 27, 27

It appears the problem didn't haven't anything to do with CGPoint but rather with the plist parameters.
This question solved my issue.

Related

ZBar not cropping scan region

I'm cropping the scanning region of Zbar via the following code:
- (void)startScanning
{
NSLog(#"Scanning..");
reader = [AACZBarViewController new];
reader.readerDelegate=self;
reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
reader.showsZBarControls = NO;
CGFloat x,y,w,h;
x =0;
y =0.25;
w=1;
h=0.50;
reader.scanCrop = CGRectMake(x,y,w,h); //Crop scan region
reader.cameraOverlayView = [self myOverlay];
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
[self presentViewController:reader animated:YES completion:nil];
}
The problem however is that the program still uses the entire screen area to find a barcode - not the middle 50%. I don't think the issue is the reader.scanCrop method, but as to what the real culprit is, I can't fathom.
Edit:
Anyone?
I had a look at the zbar documentation again and noticed it said the x axis on the camera is verticle - not horizontal. Now I'd set the reader to be portrait only but apparently this does not affect the camera in any way. I didn't find a way to change this, but I did manage to crop to the scanning region I wanted.
The solution:
If you want the following scan region (x,y,w,h) then you set the rectangle by swapping the x and y and width and height so do this (y,x,h,w). It doesn't seem to crop to the bounding box exactly, but it's close enough for my purposes.

Integer variable to label. Xcode 4.2

This is probably a noob question but I've been trying to figure it out for a few hours now and I can't. I want to make a label show the number of an integer variable.
What I've done is that I've declared a variable x as integer, I give that variable the number 100 and then I am trying to show that number to a label.
I tryed this but it isn't working.
x = 100;
label.text = [[NSString alloc] initWithFormat: #"%.2f", x];
This takes place inside a button.
Thanks in advance
You have to use '%d' instead of '%.2f'. '%f' is for float, not int.

How to print a variable in Xcode

I am new to Xcode.
I have variables x and y as random numbers from 1 to 100. I want the screen to print x and y when a button is pressed. How do I do that?
my code is:
- (IBAction)printtwonumbers:(id)sender;
{
x = arc4random() %100;
y = arc4random() %100;
label1 setText: [x];
label2 setText:[x];
}
XenElement is right if you want to print to the log. If you want to set the text of the labels though then your syntax is incorrect. In fact, if you are using that code you are probably getting a bunch of warnings and/or errors.
int x = arc4random() %10;
int y = arc4random() %100;
[label1 setText: [NSString stringWithFormat:#"%i", x];
[label2 setText: [NSString stringWithFormat:#"%i", y];
Take a look at the compiler warnings and you'll see why this code is what you need.
NSLog(#"Message Here: %d", x);
etc.
As noted by XenElement, NSLog is the basic method for printing to the console. printf also works as expected but there's little reason to use it.
I like to use Marcus Zarra's DLog macro. It gives a little more information than NSLog and it only prints to the console when you're running a debug build. The source is available on Marcus' blog.
To set the DEBUG flag, go to the build settings for the debug build and enter DEBUG under "Preprocessor Macros".

core-plot remove decimal points from axis labels

Can someone tell me how to remove the decimal points from the Axis labels? Instead of 10.0 I'd like to have only 10 showing.
CPTXYAxis *x = axisSet.xAxis;
NSNumberFormatter *Xformatter = [[NSNumberFormatter alloc] init];
[Xformatter setGeneratesDecimalNumbers:NO];
[Xformatter setNumberStyle:NSNumberFormatterDecimalStyle];
x.labelFormatter = Xformatter;
[Xformatter release];
This will take care of the decimals on the x axis as well as add commas with NSNumberFormatterDecimalStyle. You will need to do the same for the y axis.
There are a ton of things you can do with NSNumberFormatter, including converting numbers into dollars using:
[Xformatter setNumberStyle:NSNumberFormatterCurrencyStyle];
//this will add a decimal point again if you put this in the code above
Play around with the Esc key to see all formatting available for setNumberStyle or other methods.
Set the labelFormatter property on the axis to a new formatter. This is a standard NSNumberFormatter object. See Apple's class documentation for details on the options available.

Cocoa: Getting the current mouse position on the screen

I need to get the mouse position on the screen on a Mac using Xcode. I have some code that supposedly does that but i always returns x and y as 0:
void queryPointer()
{
NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSLog(#"Mouse location:");
NSLog(#"x = %d", mouseLoc.x);
NSLog(#"y = %d", mouseLoc.y);
}
What am I doing wrong? How do you get the current position on the screen?
Also, ultimately that position (saved in a NSPoint) needs to be copied into a CGPoint to be used with another function so i need to get this either as x,y coordinates or translate this.
The author's original code does not work because s/he is attempting to print floats out as %d. The correct code would be:
NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSLog(#"Mouse location: %f %f", mouseLoc.x, mouseLoc.y);
You don't need to go to Carbon to do this.
CGEventRef ourEvent = CGEventCreate(NULL);
point = CGEventGetLocation(ourEvent);
CFRelease(ourEvent);
NSLog(#"Location? x= %f, y = %f", (float)point.x, (float)point.y);
Beware mixing the NS environment with the CG environment. If you get the mouse location with the NS mouseLocation method then use CGWarpMouseCursorPosition(cgPoint) you will not be sent to the point on the screen you expected. The problem results from CG using top left as (0,0) while NS uses bottom left as (0,0).
The answer to this question in Swift
let currentMouseLocation = NSEvent.mouseLocation()
let xPosition = currentMouseLocation.x
let yPosition = currentMouseLocation.y
NSLog(#"%#", NSStringFromPoint(point));
NSLog is true;

Resources