Tracing the position of the images in the scrollview while scrolling - iphone

I ve set of images (created dynamically) which are placed in a single row in my UIScrolView.. I want to trace the changes in the locations (i.e I want to get the x,y position of all the images)while scrolling ....
Things I ve tried:
1.I stored all the images in an array
2.Then I printed the locations of images in this delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
But I can't get result what I want....Instead of that I get the positions of the images which I specified.....How can I get rid of this????
Thanks

I'm not entirely sure I understand what you mean by the location of the images but I think you want the contentOffset property of UIScrollView. If you subtract the x and y values of that from the offset of the frames of your images then you will effectively get where the images are relative to your UIScrollView's parent.
Edit: Here's some code to get you started:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
for (UIView *view in scrollView.subviews) {
CGPoint thisOrigin = view.frame.origin;
thisOrigin.x -= offset.x;
thisOrigin.y -= offset.y;
NSLog(#"thisOrigin = %#", NSStringFromCGPoint(thisOrigin));
}
}
That will get the origin of the images in the coordinate system of the UIScrollView's bounds. If you want just the origin of the images within the coordinate system of the UIScrollView's contents then you just want view.frame.origin itself. But I don't think that's what you want since you said "But I can't get result what I want....Instead of that I get the positions of the images which I specified".

Related

Checking to see if point in child view is in parent view

I have the following set-up:
Where the light blue view, let's call it parentView, has a rectangular subview (the purple view) called childView. The user can use pan touches to rotate and stretch childView by putting their finger on the point exhibited by the red dot and pushing it or pulling it.
It's possible that the childView could be scaled small enough to that after the user is finished with its touches, the point denoted by the red dot would be inside of the parentView.
My goal is to create a method that can detect if the red point is in the parentView or not. I've written the following code:
CGPoint childViewRedPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height / 2);
CGPoint rotatedChildViewRedPoint = CGPointApplyAffineTransform(childViewRedPoint, CGAffineTransformMakeRotation(self.rotateAngle));
CGPoint convertedChildViewRedPoint = [self convertPoint:rotatedChildViewRedPoint toView:self.superview];
if (CGRectContainsPoint(self.superview.bounds, convertedChildViewRedPoint))
{
return YES;
}
else
{
return NO;
}
First I find the red point as defined within the childView, then I rotate it by the amount that the view has been rotated, then I convert it to be in the parentViews coordinates.
The points I'm getting don't seem to make sense and this isn't working. Was wondering if anyone knows where I'm going wrong here? Am I not taking parentViews superview into account?
I am not 100% sure, but I think that convertPoint: already takes a rotation (or any other transformation) into account, so you only need:
CGPoint childViewRedPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height / 2);
CGPoint convertedChildViewRedPoint = [self convertPoint:childViewRedPoint toView:self.superview];
if (CGRectContainsPoint(self.superview.bounds, convertedChildViewRedPoint))
...

iPhone iOS calculate location within a UIScrollView with zooming

I have a color map within a UIScrollView and am trying to sample the color of a pixel of this map. The sample reticle is positioned above the scrollview, while the user moves the contents of the scrollview under the reticle.
The user can drop the reticle with a tap gesture, but I would like to offer an extra option of moving the view under the reticle.
I'm trying to find out how I can understand what x,y coordinate of the zoomed view is currently under the reticle. The logic for this so far eludes me, especially since zooming in/out is involved.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint mapLocation = [tapGestureRecognizer locationInView:self.surfaceMap];
NSLog(#"mapLocation (x,y) %.0f,%.0f",mapLocation.x,mapLocation.y);
NSLog(#"contentOffset (x,y) %.0f,%.0f",self.scrollView.contentOffset.x,self.scrollView.contentOffset.y);
//calculate where the marker is pointing to in the surface map while the scrollview is scrolling
int frameWidth = self.surfaceMap.frame.size.width;
int frameHeight = self.surfaceMap.frame.size.height;
//this is what I'm trying to calculate
CGPoint trueLocation = CGPointMake(self.scrollView.contentOffset.x+frameWidth-self.surfaceMap.frame.origin.x, self.scrollView.contentOffset.y-self.surfaceMap.frame.origin.y);
NSLog(#"trueLocation (x,y) %.0f,%.0f",trueLocation.x,trueLocation.y);
[self colorOfPixelAtPoint:trueLocation];
}
Any input is appreciated!
You may want to a have look at these two methods in UIView:
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

UIView's position

I have 4 views and i am drawing circles inside these views.The user is able to move these views.How can i get the position of each view relative to the window(i mean relative to 320*480)?
I want to draw and fill a polygon using the position of views.
You can use the frame property of the UIView to retrieve its location and size. See the class reference for more information:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
Example:
... = myView.frame.origin.x; //x-coord
... = myView.frame.origin.y; //y-coord
... = myView.frame.size.width; //width
... = myView.frame.size.height; //height
You can grab the position in the following way:
CGPoint positionOfAView = view.frame.origin;
or if transforms were applied:
CGPoint positionOFAView = view.bounds.origin;
Alternatively, you may want to grab the center:
CGPoint centerOfAView = view.center;
See this answer, too.
You have posted the same question again.(your first question) Before anybody answer's this question the same way it was answered earlier. Can you please tell,
Are you drawing the Circles in the view using CoreGraphics?
When you say 4 different views, how they are managed? Are they displayed at the same time?
Are there 4 View objects added on 1 ViewController?
You can get the position using
CGRect frame = [myView frame];
But remember it will send coordinates and origin based on its parent view
And after making changes
myView.frame = frame;
Hope this helps....
If you want to just move a view (without changing it size), consider using the center property, it might be more convenient.
Here's an example, assuming your circle structure is built in a certain way :)
myView.center = CGPointMake(circle.x + circle.width/2, circle.y + circle.height/2);

Find the center point of a UIScrollView while zooming

I'm having difficulties getting a tiled UIScrollView to zoom in and out correctly with pinch zooming. The issue is that when a pinch-zoom occurs, the resulting view is usually not centered in the same region.
Details: The app starts with a tiled image that is 500x500. If a user zooms in, it will snap to 1000x1000 and the tiles will redraw. For all the zoom affects, etc. I am just letting the UIScrollView do it's thing. When scrollViewDidEndZooming:withView:atScale: is called, I redraw the tiles (like you can see in many examples and other questions here).
I think that I've drilled the problem down to calculating the center of the view correctly when I get to scrollViewDidEndZooming:withView:atScale: (I can center on a known point fine after I redraw).
What I'm currently using:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
// as an example, create the "target" content size
CGSize newZoomSize = CGSizeMake(1000, 1000);
// get the center point
CGPoint center = [scrollView contentOffset];
center.x += [scrollView frame].width / 2;
center.y += [scrollView frame].height / 2;
// since pinch zoom changes the contentSize of the scroll view, translate this point to
// the "target" size (from the current size)
center = [self translatePoint:center currentSize:[scrollView contentSize] newSize:newZoomSize];
// redraw...
}
/*
Translate the point from one size to another
*/
- (CGPoint)translatePoint:(CGPoint)origin currentSize:(CGSize)currentSize newSize:(CGSize)newSize {
// shortcut if they are equal
if(currentSize.width == newSize.width && currentSize.height == newSize.height){ return origin; }
// translate
origin.x = newSize.width * (origin.x / currentSize.width);
origin.y = newSize.height * (origin.y / currentSize.height);
return origin;
}
Does this seem correct? Is there a better way? Thanks!
The way I have solved this so far is to store the initial center point of the view when the zoom starts. I initially saving this value when the scrollViewDidScroll method is called (and the scroll view is zooming). When scrollViewDidEndZooming:withView:atScale: is called, I use that center point (and reset the saved value).
The center of the scrollview can be found by adding it's center property, and it's contentOffset property.
aView.center = CGPointMake(
self.scrollView.center.x + self.scrollView.contentOffset.x,
self.scrollView.center.y + self.scrollView.contentOffset.y);

Getting x and y Position?

How to Get the X and Y axis of UIImage, I have one images which are randomly changes
it's position so how to get the image current x and y position so i can match with
another image x and y position.I have to get the position of Image without any touch
on screen. please suggest some solution.
Thank You.
You can get the frame of any view by accessing its frame property. Within that frame struct are a CGPoint origin and a CGSize size value. The origin is probably what you're looking for. Note that it is expressed in terms of relative position of the view within its superview.
For example, the following will print the origin coordinate of a view called imageView within its superview:
CGPoint origin = imageView.frame.origin;
NSLog(#"Current position: (%f, %f)", origin.x, origin.y);