Not even sure how to ask this. Transpose local coordinates to global?
The UIView class has a handful of methods for converting coordinates of CGPoints or CGRects from one view to another. Take a look at convertPoint:toView:, convertPoint:fromView:, etc in the UIView class reference.
You can convert coordinates from one view to another or from any view to the window or vice versa.
Related
I have the following design:
How should I proceed? Add a NSScrollView with the first element, which contains a couple of NSTextFields and an NSImage and then repeat in the code? What type of repeater should I use? And... is NSScrollView able to handle this type of design?
Is it possible to use NSTableView?
Thank you!
I would probably have a custom NSView class that contains the date labels on the left, a NSImage that toggles between two states (selected or not) and the request label on the right. You can lay it out either in code or in a .nib and instantiate it that way respectively.
When you create the superview create four of the new subclasses and put them in the appropriate places.
Only use scroll view if you think that you'll have so many options that you will need to scroll.
I am writing a small iPad application that draws a shape from a list of coordinates. I would like to tap anywhere inside the shape and have some action occur (i.e. NSLog proving it worked).
Does anyone know how to create a tappable area that is defined by a list of coordinates?
The shape is being drawn on top of a MKMapView.
My approach would be:
Have the points that demark the shape live within a subclass of UIView. Override pointInside:withEvent: for that class. Then look at How can I determine whether a 2D Point is within a Polygon? and use your new knowledge to implement pointInside:withEvent:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
//Left as homework
}
You can use a regular tap gesture recognizer with this :)
Only instances of UIView are tappable, and their area is defined by their rectangular property frame. In principle it would be possible (for very simple and specific shapes) to approximate the area defined by coordinates by multiple UIViews, but this is probably not what you want.
I need to draw a line across a custom UIView(i.e. from top-left to bottom-right). However, as far as I know, the coordinate systems are different.
How can I translate between those two coordinate systems?
As long as we are talking about UIView objects that are subview of some other UIView object - the quite normal case - each View is positioned in relation to its next super view. The top left corner of the superview is (0,0).
For most applications that simply works fine. In rather rare cases there may be a need to transform coordinates from one views's sub-view-coordinate-system to that of another view. UIView provides the methods
– convertPoint:toView:
– convertPoint:fromView:
– convertRect:toView:
– convertRect:fromView:
for that usage. They work quite fine. Before you make use of them you should re-think whether that is required and evaluate whether your current view hierarchy could do with a re-desin. :)
Why don't you just draw it no matter what the coordinate systems is? Don't care about the coordinate systems.
I am trying to check for a collision of an UIView that is in the main view, with a UILabel that is a subview of another view in the main view.
When I use CGRectContainsPoint, it does not return true. When I check the UILabel's frame, it returns values relative to the subview it's in, not it's absolute position. I figured this might be the problem.
If so, how do I specify that I want UILabel's absolute values for the frame?
You can convert coordinates between different coordinate systems using the NSView methods:
- convertRect:(NSRect) fromView:(NSView)
- convertRect:(NSRect) toView:(NSView)
If the second argument is nil, the coordinates are converted from/to window base coordinates. Similar methods exist for NSPoint and NSSize variables. One solution would be to convert all rectangles and points to window base coordinates and check for collision in those coordinates.
I'm trying to get the NSPoint cooridnates of an NSTextFieldCell, but NSTextFieldCell doesn't inherit from NSView, and therefore doesn't have the frame method. Any ideas on how to get around this?
I'm trying to use Matt Gemmel's MAAttachedWindow to attach little helper popups to certain elements. I want to attach it to an area on the window, and other than hacking it together by manually messing with the x and y coordinates, I'm not sure how to get the coordinates.
You can call [theCell controlView] to get a reference to the control that owns a particular cell. If the NSTextFieldCell object is part of a simple control, such as an NSTextField, the following should be sufficient:
NSRect cellFrame = [[theCell controlView] frame];
NSPoint origin = cellFrame.origin;
//..
If, however, the NSTextFieldCell is part of a more complex control, such as an NSTableView, where a single cell is used in multiple places, you will need more information in order to determine the proper rectangle. NSCell offers the method representedObject, which can help you to determine which object in the NSTableView is represented by the cell at that particular moment. Without knowing more about your specific case, I don't know how much more detail to provide in that regard.
Here is one possible solution, assuming you are able to discern the row and column information from the object stored in representedObject:
NSTableView * tableView = [theCell controlView];
id cellObject = [theCell representedObject];
NSInteger row = //... determine from representedObject
NSInteger col = //... determine from representedObject
NSRect cellFrame = [tableView frameOfCellAtColumn:col row:row];
A cell is a reusable object that's owned by an NSControl (an NSView subclass). It doesn't have an origin point because it doesn't actually represent a place on the screen, it's up to the control (which has a specific frame rectangle) to draw it where and when it's needed. Think about a table view for example, it might use a single cell that's re-drawn in several places for each row.
The cell is passed a frame rectangle by the control when it's drawn onto the screen, in most cases that should be all you need. You can also take advantage of NSCell's sizing methods, which can tell you how much room it needs to display its content.
For using MAAttachedWindow, could you instead use the control's frame (if it's a single-cell control), or in the case of a table view with a specific row one of the NSTableView layout methods? rectOfRow: or rectOfColumn:, for example. You could override NSTextFieldCell and position the window in one of the drawing methods, but I'd save that for my absolute last choice if possible.
It doesn't have co-ordinates. There are none to get. The way cells work is that the view tells the cell “draw here”.
So, if you're not in one of the cell's drawing methods and you really, really need co-ordinates, you need to ask the view that owns the cell. Usually, this is [cell controlView].
(Perhaps you're thinking of UIKit? UITableViewCells are very different from NSCells.)