I have a pan gesture which returns the current location (in x and y coordinates) of the user.
I also have different sized circles which are stacked up on each other.
My question now is, is it possible to find one of those circles (each one is an UIView) by a x,y coordinate? Or how does one find out which UIView's are at the users current location?
Brute Force:
NSArray *allCircle /*collection of UIviews */
for(UIView *circle in allCircles){
if(CGRectContainsPoint([circle frame], point))
{
// got it !!
break;
}
}
Related
Is there a way to know if a 'tap' is inside or outside the masked area of a UIView? I'm using CoreGraphics to mask the UIView.
So far my code goes something like this..
- (void)viewDidLoad {
UIGestureRecogniser *r = [[UIGestureRecogniser alloc] initWithTarget:self action:#selector(gestCall:)];
[self addGestureRecogniser:r];
}
- (void)gestCall:(UIGestureRecogniser *)gestRec {
if ("somthing") {
// outside of mask
} else {
// inside of mask
}
}
Thank you.
I've finally found the solution I was looking for. So for the benefit of any one trying to find is a CGPoint is inside any CGPath.
It's simple.
UIBezierPath *p = [UIBezierPath bezierPathWithCGPath:anyCGPath];
BOOL isInPath = [p containsPoint:anyCGPoint];
Basically you need to check the touch coordinate and decide whether is falls into the mask area or not. Override the hitTest:withEvent: and account for the image mask. You can use [[[self layer] presentationLayer] hitTest:aPoint] or [[[self layer] mask] hitTest:aPoint] in your overridden `-[UIView hitTest:withEvent:].
[EDIT]
Check if a user tapped near a CGPath might help to find answer to your question.
[EDIT]
Do following in your Gesture Handler to figure out to process tap or not.
Specify the center of the circle (This would be UIView.Center as CGPoint)
Specify the radius of pie chart
When user tap on the view, get the location as point - CGPoint and calculate point.x*point.x+point.y*point.y (Circle formulae) and this value must be less than or equal to the square of the radius i.e radius*radius. If this condition satisfied then your tap point is inside the circle otherwise outside.
Hope that makes clear.
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;
i want to draw the pin and information of the place on the image of the camera..
Please any one help me..
i had done the coding in the app delegate
The code is :-
overlay = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; overlay.opaque = NO; overlay.backgroundColor=[UIColor clearColor];
[window addSubview:overlay];
#define CAMERA_TRANSFORM 1.24299
UIImagePickerController *uip;
#try {
uip = [[[UIImagePickerController alloc] init] autorelease];
uip.sourceType = UIImagePickerControllerSourceTypeCamera; uip.showsCameraControls = NO;
uip.toolbarHidden = YES;
uip.navigationBarHidden = YES;
uip.wantsFullScreenLayout = YES;
uip.cameraViewTransform = CGAffineTransformScale(uip.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
}
#catch (NSException * e)
{ [uip release];
uip = nil;
}
#finally
{ if(uip) {
[overlay addSubview:[uip view]]; [overlay release]; }
}
it shows the camera.Not i want to detect the place and put the pin on that place which shows the information of that place.
Here is a more straightforward recipe to detect the presence of a camera:
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
99% of the job is still ahead I'm afraid. Roughly you need the following:
Get the geographical location for the user.
Get the geographical location for the point of interest (POI) you want to show. You may need to use a 3rd party library like Foursquare, Google maps, or something like that.
Calculate the distance between the user and a POI using a right triangle between both points h^2=c^2+c^2. Note that the distance in spherical geometry is calculated with the haversine formula, but the loss of precision is irrelevant for small distances if we assume cartesian coordinates, so we will just do that.
Assuming that east is 0º, get the angle from the user to the POI, which is atan dy/dx (y=latitude, x=longitude). dy is of course, the difference between the latitudes from the user and the POI.
Get the bearing from the compass and calculate the difference between the user bearing and the angle to the POI.
The position on screen of the object depends on the bearing and the device orientation. If the user is looking exactly at the POI, paint a label for the POI in the middle of the screen. If there is an offset from the exact angle, multiply offset * (width in pixels / horizontal field vision) to get the offset in pixels for the label representing the point. Do the same for the vertical offset.
If there is a rotation on the axis X (see the axis here), apply a vertical offset.
If there is a rotation on the axis Y, there will be an update in bearing from the compass.
If there is a rotation on the axis Z, if the object is near rotate the object in the opposite angle.
Scale the label according to the distance, with a minimum and a maximum.
To position the labels you may want to use a 3D engine or rotate them in a circle around your device (x=x+r*cos, y=y+r*sin) and use a billboard effect.
If that sounds like too much work, concentrate on implementing just the response to changes in bearing using offset * width in pixels / horizontal field vision. Horizontal field vision is the visible angle for the camera. It is 180º for humans, 37.5 for iPhone 3, and hmm was it 45º for iPhone 4? Width is 320, so if you are looking 10º away from your target, you have to move it horizontally 320*10/37.5 pixels away from the center.
If the readings from the compass have too much noise, add a low pass filter.
Please go through
https://github.com/zac/iphonearkit.
It's the best objectiveC code available.
Im wondering if there is anyway that you can get the absolute location of a control in a ipad/iphone application. e.g. I have a TextField which is within a child of a child of a view. I want to know the X and Y values in relation to the top Parent View (e.g. currently the x and Y of the textfield return 10 and 10 because that is it's frame location within its own view, but I want to know this in relation to its parent which should be something like X = 10 and Y = 220). I need to make a generic method for this somehow. Hope this make sense.
Any ideas?
You are looking for -[UIView convertPoint:toView:].
For example, to get the origin of a view view in terms of its window's base coordinates, you would write:
CGPoint localPoint = [view bounds].origin;
CGPoint basePoint = [view convertPoint:localPoint toView:nil];
If you instead want to convert the point into the coordinate system of some other view within the same window, you can use that view as the toView: argument instead of nil:
NSAssert([view window] == [otherView window],
#"%s: Views must be part of the same window.", __func__);
CGPoint otherPoint = [view convertPoint:localPoint toView:otherView];
Because different coordinate systems can have different scales, you might find -convertRect:toView: to be more useful, depending on what you're planning to do with the coordinates. There are also analogous -fromView: versions of both the point and rect conversion methods.
If I start with a UIImageView, and I add a subview, how do I translate a coordinate in the original UIImageView to a corresponding coordinate (the same place on the screen) in the subview?
UIView provides methods for exactly this purpose. In your case you have two options:
CGPoint newLocation = [imageView convertPoint:thePoint toView:subview];
or
CGPoint newLocation = [subview convertPoint:thePoint fromView:imageView];
They both do the same thing, so pick whichever one feels more appropriate. There's also equivalent functions for converting rects. These functions will convert between any two views on the same window. If the destination view is nil, it converts to/from the window base coordinates. These functions can handle views that aren't direct descendants of each other, and it can also handle views with transforms (though the rect methods may not produce accurate results in the case of a transform that contains any rotation or skewing).
Subtract the subview's frame.origin from the point in the parents view to the same point in the subview's coordinate:
subviewX = parentX - subview.frame.origin.x;
subviewY = parentY - subview.frame.origin.y;
Starting with code like:
UIImageView* superView=....;
UIImageView subView=[
[UIImageView alloc]initWithFrame:CGRectMake(0,0,subViewWidth,subViewHeight)
];
subView.center=CGPointMake(subViewCenterX, subViewCenterY);
[superView addSubview:subView];
The (subViewCenterX, subViewCenterY) coordinate is a point, in superView, where the center of subView is "pinned". The subView can be moved around wrt the superView by moving its center around. We can go, for example
subView.center=CGPointMake(subViewCenterX+1, subViewCenterY);
to move it 1 point to the right. Now lets say we have a point (X,Y) in the superView, and we want to find the corresponding point (x,y) in the subView, so that (X,Y) and (x,y) refer to the same point on the screen. The formula for x is:
x=X+subViewWidth/2-subViewCenterX;
and similarly for y:
y=Y+subViewHeight/2-subViewCenterY;
To explain this, if you draw a box representing the superView, and another (larger) box representing the subView, the difference subViewWidth/2-subViewCenterX is "the width of the bit of the subView box sticking out to the left of the superView"