how to detect free space around object - objective-c

i've got few objects in view and i want to detect if space (for ex. [myObject frame] - 5px) is free.
now i must detect when my object is clicked but how can i detect free space?
NSString *currPuzzle = [NSString stringWithFormat:#"%d", currentPuzzle];
currentObject = [puzzleArray objectForKey:currPuzzle];
currentObjectFrame = [currentObject frame];
currentObjectCenter = [currentObject center];
int left = currentObjectFrame.origin.x - 1;
int right = currentObjectFrame.origin.x + 1;
int top = currentObjectFrame.origin.y - 1;
int bottom = currentObjectFrame.origin.y + 1;
i've got no idea how solve it.
any idea?

ok i resolve it. i create temporary rectangle and i detect intersect with this rect. all work great.

Related

sprite kit - objective c: slow fps when I create a lot nodes

I wanted to create a space background so I make a for loop to create the stars. Here is the code:
for (int i = 0; i<100; i++) {
SKShapeNode *star= [SKShapeNode shapeNodeWithPath:Path.CGPath];
star.fillColor = [UIColor whiteColor];
star.physicsBody = nil;
int xposition = arc4random()%960;
int yposition = arc4random()%640;
star.position = CGPointMake(xposition, yposition);
float size = (arc4random()%3 + 1)/10.0;
star.xScale = size;
star.yScale = size;
star.alpha = (arc4random()%10 + 1 )/ 10.0;
star.zPosition = -2;
[self addChild:star];
}
But it takes a lot from my cpu. when the code is activated the cpu at top 78%.(I check the code in the iPhone simulator);
Somebody know how to fix it? thanks.
Your physics bodies continue to calculate even when off of the screen. You will need to remove them once they go out of the frame, otherwise everything will slow to a crawl. (And to echo what others have stated you will eventually need a real device).
From this document: Jumping Into Sprite Kit
You can implement the "Did Simulate Physics" method to get rid of the stars that fell from the bottom of the screen like so:
-(void)didSimulatePhysics
{
[self enumerateChildNodesWithName:#"star" usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.y < 0)
[node removeFromParent];
}];
}
Note that you will first need to set the name of your star shapes by using the name property like so:
star.name = "star"

WebView page count

I have a WebView that displays some content, which is frequently updated. I would like to display a page counter in my UI which would tell the user how many pages would web view have if they would to print it.
I have tried doing:
NSRange r = NSMakeRange(0, 0);
BOOL knows = [[[[webView mainFrame] frameView] documentView] knowsPageRange: &r];
which gives knows = YES but r.length always equals 1.
How can I get at this information? (Preferably in a performant way, if possible).
Not a perfectly elegant solution, but a possibility could include:
NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"]; // Get the height of our webView
int height = [heightStr intValue];
CGFloat maxHeight = kDefaultPageHeight - 2*kMargin;
CGFloat maxWidth = kDefaultPageWidth - 2*kMargin;
int pages = ceil(height / maxHeight);
return pages;
via Chris Mills' blog.

IKImageBrowserView Moving items doesn't animate

I have an IKImageBrowser setup which appears to be working well. I have set it up to allow reordering and also set animation to YES (in my awakeFromNib), however whenever I select and try and reorder the images I get strange behaviour:
1) They don't reorder most of the time
2) If they do they don't animate
3) Sometimes the images land on each other, if I scroll away and back they are back where they started.
If I highlight an image and delete it, it animates and disappears as expected...
Is this a problem with Core Animation? Do I need to add a core animation layer to the object in interface builder? I followed this tutorial from Apple to get these results: http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/ImageKitProgrammingGuide/ImageBrowser/ImageBrowser.html#//apple_ref/doc/uid/TP40004907-CH5-SW1
Here's the code in question:
- (BOOL) imageBrowser:(IKImageBrowserView *) aBrowser moveItemsAtIndexes: (NSIndexSet *)indexes toIndex:(NSUInteger)destinationIndex{
int index;
NSMutableArray *temporaryArray;
temporaryArray = [[[NSMutableArray alloc] init] autorelease];
for(index=[indexes lastIndex]; index != NSNotFound;
index = [indexes indexLessThanIndex:index])
{
if (index < destinationIndex)
destinationIndex --;
id obj = [mImages objectAtIndex:index];
[temporaryArray addObject:obj];
[mImages removeObjectAtIndex:index];
}
// Insert at the new destination
int n = [temporaryArray count];
for(index=0; index < n; index++){
[mImages insertObject:[temporaryArray objectAtIndex:index]
atIndex:destinationIndex];
}
return YES;
}
Interestingly, this line throws a warning
for(index=[indexes lastIndex]; index != NSNotFound;
comparison is always true due to
limited range of data type
As mentioned above by NSGod, changing int index to NSUInteger index solved the problem

How do I get the details of an application using Objective-C?

I have the following code to detect the current window. How can I get 1) application internal name, 2) location, 3) publisher and 4) description of the window/application?
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//Get info about the currently active application.
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSDictionary* currentAppInfo = [workspace activeApplication];
//Get the PSN of the current application.
UInt32 lowLong = [[currentAppInfo objectForKey:#"NSApplicationProcessSerialNumberLow"] longValue];
UInt32 highLong = [[currentAppInfo objectForKey:#"NSApplicationProcessSerialNumberHigh"] longValue];
ProcessSerialNumber currentAppPSN = {highLong,lowLong};
//Grab window information from the window server.
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
ProcessSerialNumber myPSN = {kNoProcess, kNoProcess};
//Loop through the windows, the window list is ordered from front to back.
for (NSMutableDictionary* entry in (NSArray*) windowList)
{
int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
GetProcessForPID(pid, &myPSN);
//If the process of the current window in the list matches our process, get the front window number.
if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN)
{
NSNumber *windowNumber = [entry objectForKey:(id)kCGWindowNumber];
windowNumber = [entry objectForKey:(id)kCGWindowNumber];
NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
NSLog(#"Capture the window: %# with window ID: %#.",applicationName,windowNumber);
return applicationName;
//Break because we only want the front window.
break;
}
}
CFRelease(windowList);
[pool release];
You should use the ProcessInformationCopyDictionary function from the Process Manager API. Give it &myPSN and kProcessDictionaryIncludeAllInformationMask as arguments and you will get the information you are looking for.
I was looking for something related with this topic. I need a WindowRef of the window or window part at a certain location (mouse position) and it has to be over all the windows of all running applications...
I´ve tried it with Carbon (´Cos my App is entirely written in C++) but I´ve found that Some Carbon Functions Doesn´t work properly (MacFindWindow, FindWindow, HIWindowFindAtLocation, FindWindowOfClass, HIWindowGetCGWindowID...)
Maybe I´m doing it wrong, It´s difficult to believe that those Carbon functions won´t work any more in 64 bits architectures...
So, related with your question I found the same code and I tried this but it isn´t what I need, I hope it helps you in any way and I´ll keep searching and trying till I get it (If the O.S can do it everybody should).
//if the process of the current window in the list matches our process, get the front window number
if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN)
{
NSNumber* windowNumber = [entry objectForKey:(id)kCGWindowNumber];
NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
NSLog(#"The current app is %# and the window number of its front window is %#.",applicationName,windowNumber);
CGRect bounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[entry objectForKey:(id)kCGWindowBounds], &bounds);
NSLog(#"WINDOW RECT BOUNDS; (x,y,width, height) = (%d,%d, %d, %d)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
break;
}
Also, follow this link, I´t will help you. I´m sure:
http://code.google.com/p/blazingstars/source/browse/trunk/PokerHK/HKLowLevel.m?r=70

Efficiently Access numerous Cocoa Controls

I have an interface that has large numbers of controls, see image below.
Interface http://www.richardstelling.com/hosted/cocoainterface.png
What is the best way to access these, creating 288 IBOutlets in my AppController class and linking them all up seems inefficient.
I looked at forms, but they seemed to simplistic.
This is a proof-of-concept and will not ship so I'm open to any ideas. One caveat however I have to use Objective-C as the final product will be written in Objective-C/Cocoa.
NB:
The interface is static
Smaller boxed will hold integers (0-255)
You should look into NSMatrix. This is exactly what it's designed to solve.
NSTableView looks like the UI you need. The visual rendering will be a bit different but it will look more 'Mac'.
Either NSMatrix, as Rob suggests, or re-think the UI so you have fewer controls on it :-)
You could build the entire interface programmatically, with a few lines of code in a loop:
const int numRows = 11;
const int rowWidth = 400;
const int rowHeight = 20;
const int itemSpacing = 5;
const int nameFieldWidth = 120;
const int smallFieldWidth = 30;
NSMutableArray * rowList = [[NSMutableArray alloc] initWithCapacity:numRows];
int rowIndex;
NSRect rowFrame = [controlView bounds];
rowFrame.origin.y = rowFrame.size.height - rowHeight;
rowFrame.size.height = rowHeight;
NSRect itemRect
for (rowIndex = 0; rowIndex < 11; rowIndex++)
{
// create a new controller for the current row
MyRowController * rowController = [[MyRowController alloc] init];
[rowList addObject:rowController];
[rowController release];
// create and link the checkbox
itemRect = rowFrame;
itemRect.size.width = 20;
NSButton * checkBox = [[NSButton alloc] initWithFrame:itemRect];
[controlView addSubview:checkBox];
[rowController setCheckBox:checkBox];
[checkBox release];
// create and link the name field
itemRect.origin.x += itemRect.size.width + itemSpacing;
itemRect.size.width = nameFieldWidth;
NSTextField * nameField = [[NSTextField alloc] initWithFrame:itemRect];
[controlView addSubview:nameField];
[rowController setNameField:nameField];
[nameField release];
// create and link the smaller fields
itemRect.origin.x += itemRect.size.width + itemSpacing;
itemRect.size.width = smallFieldWidth;
NSTextField * smallField_1 = [[NSTextField alloc] initWithFrame:itemRect];
[controlView addSubview:smallField_1];
[rowController setSmallField_1:smallField_1];
[smallField_1 release];
//.. continue for each item in a row ..
// increment the main rectangle for the next loop
rowFrame.origin.y -= (rowFrame.size.height + itemSpacing);
}