I am displaying a CATiledLayer via a scrollview which displays a map. When the user presses a button, I want to determine the bounds of the catiledlayer that the user is currently viewing and place an image there.
In other words, I need to determine the portion of the CATiledLayer that is being displayed, then determine the center point of that portion.
Appreciate any thoughts.
You can call scrollView.contentOffset (which returns a CGPoint) which will give you where all the content in your scroll view is in relation to the origin point of the scroll view. So if your CATiledLayer is up and to the left you'd get some thing like (-50, -100). Take those values, multiply by -1 and then add them to scrollView.center property (also returns a CGPoint). This will give you a CGPoint coordinate that if used as the center point of a new-subview within scrollView will center that view within the current frame.
CGPoint currentOffset = CGPointMake(scrollView.contentOffset.x * -1, scrollView.contentOffset.y * -1);
CGPoint center = CGRectMake(scrollView.center.x + currentOffset.x, scrollView.center.y + currentOffset.y);
I haven't tested this code, but it should work or at least give you a general starting point!
Related
I have a problem I've run into with the UIView method convertRect: fromView: method. Here is the situation:
I have an overwritten the UIView class to create a view that rotates with the user's movement(very similar to the TaskRabbit spinner). To create the rotation, over I added an additional view to my subclassed view, and I rotated that view. The rotated view contains additional subviews that obviously rotate with the rotated subview. The problem is, after the subview has been rotated, I need to find where those additional subviews are, with respect to the original overritten view - not the rotated view. To do this, in my UIView class, I have the following:
[self convertRect:currentView.frame fromView:rotationView];
However, when I print out the frame of the converted rect, the coordinates are not accurate. Has anyone run into this issue where the convertRect: fromView: isn't accurate after the view is rotated?
Edit
Specifically, about the points being not accurate, I can't even see the relationship between what is should be and what it is-ie off by an specific angle, x/y flipped etc. For example, the point that should be (25, 195) is returned at (325.25, 273.16)
I'm assuming that you are rotating your views by applying a transform to them (either a CGAffineTransform to the view or a CATransform3D to the layer). This is what is causing the problem with your frame. The documentation for UIView frame says:
Warning If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
As you've already seen, the value of the frame is undefined. You can still use the center and bounds properties though.
I'm trying to add a box to the centre of the screen in a zoom view. E.g. if I move into an area of the content view and try using the offset coordinates, it becomes erratic if I zoom in or out. I can't seem to figure out the right mathematical formula for this.
If you are working with a UIView or one of it's subclasses. You'll always have a center property available for you. That property is a CGPoint and you can do something like this to test if it is the required result you seek:
CGPoint center = [YourUIViewOrSubclass center];
NSLog(#"Center x is '%f' and y is '%f', center.c, center.y);
I hope this helps you. Otherwise try and rephrase your question and include a little context.
I have UIView which have subviews , I have rotated view with 90.
CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadians(degrees));
Now I need to subview location in screen coordinate system and for that I am doing
CGPoint subViewPoint = [[subView superview] convertPoint:subView.frame.origin toView:baseView];
This is working fine , if I am not rotating the View but its not working if i am rotating the view. Please help me on this.
How to get the subview location in screen after its super view 90 rotation.
I'm not sure, but there are two things to know.
1) You should avoid using frame property when you have to deal with transformations. Use center and bounds properties instead. Frames of views that have not identity transformations are invalid.
2) You can always use CGPointApplyAffineTransform function to calculate point coordinates by yourself : )
Hope this'll help.
Sorry guys, I hate asking dumb questions but I have seriously been searching for days online to no avail. Every method I've tried with item.bounds or the like has failed miserably. I'm just trying to move some elements, like a UILabel and a UIButton over a few pixels under a certain circumstance. Just a simple point to a tutorial that I missed would be so helpful.
Generally frame is what you want. It is specified in terms of parent view coordinates. So if my view's frame is CGRectMake(10.f,20.f,50.f,60.f) it appears in the parent's coordinates at x=10 y=20 with width 50 and height 60.
UIView* someView ...
CGRect someViewFrame = someView.frame;
someView.frame.origin.y += 10;
someView.frame = someViewFrame;
moves the view down by 10 pixels.
If you just want to move the view in a superview, leave bounds alone.
This example should be useful.
Using the information here: iPhone Landscape-Only Utility-Template Application I am able to launch, use and maintain my view as landscape only. However, I am confused about the axis.
The absolute axis behaves as expected, meaning (0,0) is the top left, (240,0) is the top center, (0,320) is the bottom left corner, etc. However, when I attempt to do calculations related to the view I am drawing on I find x,y to be oriented as if the portrait top left were the origin. So to draw something at the center-point in my view controller I need to do:
CGPoint center = CGPointMake(self.view.center.y, self.view.center.x);
I assume this due to the fact that the UIView referenced by my controllers self.view is giving it's values relative to it's enclosing frame, meaning the window which has it's axis origin on the top left in portrait mode.
Is there some simple way to account for this that I am missing?
Documentation suggests that the transform property is exactly what I am looking for, however, I experiencing further confusion here. There are 3 essential properties involved:
frame
bounds
center
If I do this in viewDidLoad:
// calculate new center point
CGFloat x = self.view.bounds.size.width / 2.0;
CGFloat y = self.view.bounds.size.height / 2.0;
CGPoint center = CGPointMake(y, x);
// set the new center point
self.view.center = center;
// Rotate the view 90 degrees counter clockwise around the new center point.
CGAffineTransform transform = self.view.transform;
transform = CGAffineTransformRotate(transform, -(M_PI / 2.0));
self.view.transform = transform;
Now according to the reference docs, if transform is not set to the identity transform self.view.frame is undefined. So I should work with bounds and center.
self.view.center is correct now, because I set it to what I wanted it to be.
self.view.bounds appears unchanged.
self.view.frame appears to be exactly what I want it to be, but as noted above, the reference claims it is invalid.
So while I can get what I believe to be the right numbers, I fear I am overlooking something critical that will become troublesome later.
Thanks.
With Quartz 2D, the coordinate system has its origin (0,0) at the lower-left corner of the graphic, not at the upper-left corner. Maybe that's what you are missing.
See: http://developer.apple.com/documentation/graphicsimaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html#//apple_ref/doc/uid/TP30001066-CH202-CJBBAEEC