Current location of the object inside UIScrollView - iphone

I created UIScrollView and added large image inside of the scrollview to scroll. My question is how should I know the current location of the image that I added inside of the scrollview like its X and Y? Like for example, I scroll the image horizontally then and I want to know what its new X and Y value of the image.

This is going to take a little bit of math. There are two properties (both CGPoints) that are of importance to you:
The frame.origin of your image, which is its position within its superview
The contentOffset of the scroll view, which is the visible portion of the scroll view
Basically what you want to do is grab the frame.origin.x and frame.origin.y properties of the image view, which will tell you where the image is relative to the absolute origin of the scroll view. Then, get the contentOffset.x and contentOffset.y of the scroll view and subtract them from the x and y of the image view. That should provide you the relative origin of the image view to your screen.

See scrollview.contentOffset. That should have the coordinates of the point that is visible in the top-left corner of the scrollview after any scrolling.

Related

In a zoomable scrollview, how to determine the location of the content wrt the window?

I have a zoomable UIScrollView which contains a custom drawn view (two circles). When the view is zoomed, I want to know where the centres of the circles lie, wrt the App's window. Consider these steps:
The view at 1X
The view zoomed at an arbitrary level, with the pinch focused on the first circle
The view zoomed at an arbitrary level, with the pinch focused on the top right corner
What I am meaning to ask is, if I were to lay another view containing a circle on top of the scroll view such that the circle in this view is concentric (need not be of the same size) with the smaller circle in the scroll view's view, where should the centre of the new circle be located?
When using UIView's coordinate conversion methods it is not relevant that you custom view is embedded in a scroll view:
MyCustomCircleView *circleView;
CGPoint centerInLocalCoordinates = circleView.centerOfACircle;
CGPoint centerInWindowCoordinates = [circleView convertPoint:centerInLocalCoordinates
toView:nil];
Maybe window coordinates are not what you are looking for. Remember that the window's coordinate space is always in portrait orientation and with the home button at the bottom. When holding the device in landscape orientation, your coordinates are (of course) rotated.
It might be easier to convert to a superview's coordinates, for example your view controller's view's coordinate space.
Did you draw the circles? Some combination of scrollView.contentOffset, scrollView.contentSize, and scrollView.zoomScale is what you want. You can walk back the translation and scaling using those values.

iOS How to get the content offset of a view inside a scrollview

Lets say I add 10 views to a scrollview. I obviously give them a rect inside of the scrollview. Is there any way to figure out what each item's offset is in the scrollview?
I'm guessing that probably isn't applicable here. What I need in essence is, "If this item is at this content offset in the scrollview (meaning visible to the user in a certain area of the scrollview), then do this".
Each view's frame is expressed in its superview coordinate system (bounds). That is, in your case for each view its content offset is CGRectGetMinY(view.frame).
You might want to find the current scroll position by looking at UIScrollView contentOffset
The point at which the origin of the content view is offset from the origin of the scroll view.
With the contentOffset, you can then compare this with the frame x and y coordinates of the content views and determine if they are onscreen or not.

ScrollView offsets when superview is setframe?! (iphone)

So i have a View and a scroll view inside that view. I call a
[View setFrame:CGRectMake(55,70,260,420)];
i put a
NSLog(#"%f %f",scrollview.frame.origin.x, scrollview.frame.origin.y);
before and after the setFrame and it reads 6,112 and then 6,172. 112 is correct, i have no idea where the 60 comes from. i searched 60 in my implementation file, and there is nothing that could affect the y at all. is there some rule with doing a setframe when there is a view inside that view?
Thanks
If you take a look at the UIView reference, the frame property states
frame
The frame rectangle, which describes the view’s location and size in
its superview’s coordinate system.
#property(nonatomic) CGRect frame
Discussion
This rectangle defines the size and position of the view in its
superview’s coordinate system. You use this rectangle during layout
operations to size and position the view. Setting this property
changes the point specified by the center property and the size in the
bounds rectangle accordingly. The coordinates of the frame rectangle
are always specified in points.
So i guess when you NSLog the frame for scrollview it is displayed based on the coordinates of the superview. The second NSLog shows different because the superview's coordinate system has changed.
I fixed this by making the scrollview in my code instead of doing it through the XIB. the scrollview is created after the setFrame and addSubView of the parent View

Understanding UIScrollView

I'm trying to understand how UIScrollView works for zooming.
I was trying to rotate a UIImageView within a zoomed UIScrollView and I ended up with weird sizes, my centering in the scrollview not working anymore.
I solved the problem by setting the zoomScale to 1.0 before doing the rotation, and then by resetting it back to the previous value once the transformation was done.
I was wondering what was the impact of the UIScrollView on its subviews. Does it change their sizes, their positions. Or is it simply the UIScrollView that handles the zooming and the drawing of the zoomed subviews.
I realise that is an old question, but I thought I'd add additional information for those still arriving here like myself.
The scroll view manages its contents view, it zooms by adding tranformations to the content. So, for an image when you zoom in, it uses a tranformation to scale the image to the required zoom level.
When you are panning/scrolling a ScrollView, it changes the zoom level and origin positions to move the content around. So the content may well be larger than the scrollview itself. It clips the content at the scrollviews bounds and just adjusts this origin position.
The central notion of a UIScrollView object (or, simply, a scroll view) is that it is a view whose origin is adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application’s main window. A scroll view tracks the movements of fingers and adjusts the origin accordingly. The view that is showing its content “through” the scroll view draws that portion of itself based on the new origin, which is pinned to an offset in the content view. The scroll view itself does no drawing except for displaying vertical and horizontal scroll indicators. The scroll view must know the size of the content view so it knows when to stop scrolling; by default, it “bounces” back when scrolling exceeds the bounds of the content.
Source Documentation

Problem with UIScrollView

Sorry for long winded post.
I am trying to understand UIScrollView and running into very simple problem.
I am creating a scroll view
I am making this view 1.5 size larger then normal size
Using UIScrollView I expect to see some edge elements of view out of bounds, but should be able to pan the view therefore bringing missing elements back to the visible area.
However I am seeing that I can't just pan/scroll view anyway I want, instead view always wants to scroll up, as soon as move away my finger from the screen (touch end event).
I am not handling any touches, etc - I just want to understand why does not scaled view stay put where I scroll it?
CGRect viewFrame = self.view.frame ;
viewFrame.size.width *= 1.5;
viewFrame.size.height *= 1.5;
CGSize mySize = viewFrame.size;
[ ((UIScrollView *) self.view) setContentSize: mySize];
self.view.transform = CGAffineTransformMakeScale(1.5, 1.5);
What I really trying to accomplish is something similar to Number on iPad (the same code will work on iPhone):
There is a view with lots of controls
on it (order entry form)
User can zoom into the entire form so all elements look bigger
user can pan the form therefore bringing various elements into the visible area of the screen.
It seems that UIScrollView can should be able to handle zoom and pan actions (for now I am using Affine Transform to zoom in to the order entry form and iPad)
Thanks
When you transform a view, you transform its internal coordinate system as well. This means that if you scale a view, the view still "thinks" it is the same size it was before the scale because its coordinate units scaled as well.
For example, if you have an image view that has a size of (50,50) and you transform it so that it covers (200,200) on the screen, when you ask the image view its size it will report that its size is still (50,50).
Scrollviews are unusual types of views because they have understand their absolute size relative to physical device screen in order to work properly. When you transform their coordinate system, they lose that connection to the physical device screen and can no longer function properly. This is what you are seeing.
I haven't done this but I'm pretty sure to create the illusion of a zoom in a scrollview, you increase the frame of the scrollview and then transform its subviews (or transform the subviews and then increase the frame of the scrollview to contain the new subview size.) That is the only way to keep the scrollview in sync with the physical device screen.