Find out, that the touch position is in Imageview - ios

i try to run some code, when my finger is moved to the same position, where the imageview is.
I defined the position of the image view:
CGRect imagepos = myImage.frame;
And the position of actual touch position:
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];
I tried withCGPointEqualToPoint it not worked. Then i tried with CGRectContainsPoint it also not worked, because, when imagepos and currentpos have the same x coordinates, my "some code" is running (my finger is not moved to the image, they had only the same x positions. Y positions are totally different).
How can I say, when my finger (also in the method touchesmoved) touch/is moved to/is at anywhere of the image, run some code or a method?
Thanks for Answers.

In your touchesBegan method, you can try something like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([touch view] == myImgView)
{
NSLog(#"My Image was touched");
}
}
This tells you when your ImageView is touched. Make sure you set userInteractionEnabled to YES in your imageView.

Hey try out below -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self]; //Now you got current location of click or tap
if(location.y == YOUR_IMAGE_VIEW_POSITION_Y)
{
NSLog(#"Got the TOUCH...OOOoopsss !!!");
}
}
}
Hope this helped.

There is a function...
CGRectContainsPoint(rect, point);
It returns a bool if the point is inside the rect.
So get the touch point and run this with the imageView frame to find out if the touch was inside the image view.

Related

Sprite Kit detect position of touch

I want to run different actions depending on if you touch the left or right half of the screen. So an action when you touch the left half, and another when the right half is touched.
I suppose I could make a transparent button image that covers half of the screen and use this code to run an action when touched, but I don't think that's the way to go. Is there a better way to detect the position of a touch?
Override the touchesEnded:withEvent or touchesBegan:withEvent: method to do this:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
if([touch locationInView:self.view]>self.size.width/2){
[self runTouchOnRightSideAction];
}else{
[self runTouchOnLeftSideAction];
}
}
}
I came up with the following code and it works perfect.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
// If left half of the screen is touched
if (touchLocation.x < self.size.width / 2) {
// Do whatever..
}
// If right half of the screen is touched
if (touchLocation.x > self.size.width / 2) {
// Do whatever..
}
}

UIImageView Jumps when Touch Event Occurs

Okay basically what this code currently does is drag an image up and down along the Y axis depending on where the user drags it, and it returns to its original position. My problem is that if someone were to not touch directly on the center of the UIImageView and start dragging it would jolt (very not smooth). Wherever someone is touching the UIImageView and starts dragging the UIImageView jolts a little to go directly on the center of the touch event.
I was thinking about using animation just to move it where the image needs to go, or is there another way?
I apologize if this is an inefficient way to do this. I'm fairly new to the IOS world.
Here's what I have:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//Gets location of UIImageView.
self.originalFrame = self.foregroundImage.frame;
}
//This method is used for moving the UIImageView along the y axis depending on touch events.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if([touch view]==self.foregroundImage) {
CGPoint location = [touch locationInView:self.view];
location.x=self.foregroundImage.center.x;
self.foregroundImage.center=location;
}
}
//This method sets the UIImageView back to its original position.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect newFrame = self.foregroundImage.frame;
newFrame.origin.y = self.originalFrame.origin.y;
[UIView animateWithDuration:1.1 animations:^{
self.foregroundImage.frame = newFrame;
}];
}
You also need to save the first location in touchesBegan, relative to the parent view. Then, you can use that to change the frame by the difference between the previous location and the new location. Please see the following code.
- (void) touchesBegan: (NSSet*) touches
withEvent: (UIEvent*) event
{
if (touches.count == 1)
{
UITouch* touch = [touches anyObject];
self.touchLocation = [touch locationInView: self.view];
}
}
- (void) touchesMoved: (NSSet*) touches
withEvent: (UIEvent*) event
{
if (touches.count == 1)
{
UITouch* touch = [touches anyObject];
CGPoint newTouchLocation = [touch locationInView: self.view];
if (touch.view == self.foregroundImage)
{
/* Determine the difference between the last touch locations */
CGFloat deltaX = newTouchLocation.x - self.touchLocation.x;
CGFloat deltaY = newTouchLocation.y - self.touchLocation.y;
/* Offset the foreground image */
self.foregroundImage.center
= CGPointMake(self.foregroundImage.center.x + deltaX,
self.foregroundImage.center.y + deltaY);
}
/* Keep track of the new touch location */
self.touchLocation = newTouchLocation;
}
}

Is it possible to find all the subviews that exist at a point on iphone?

I would like to be able to retrieve info on a tap, about all the different subviews that are placed in that tap spot. For example, if there was a background, and in front of it was a game character, I would like to be able to tap on that character, and it will retrieve the info saying that both the character and the background are in that tap spot.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//retrieve info about what other subviews lie behind at this point
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint p=[touch locationInView:self.view];
NSLog(#"Coordiantes of Tap Point:(%f,%f)", p.x, p.y);
NSArray *anArrayOfSubviews = [self.view subviews];
NSMutableArray *requiredSubviewArray = [NSMutableArray array];
for (UIView *aSubView in anArrayOfSubviews){
if(CGRectContainsPoint(aSubView.frame, p)) {
//aSubView is there at point 'p'
[requiredSubviewArray addObject:aSubView];
}
}
// requiredSubviewArray contains all the Subviews at the Tap Point.
}
try this.it maybe helpful to you..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//retrieve info about what other subviews lie behind at this point
NSLog(#"subviews--%#",[self.view subviews]); //it prints all subviews as Array
NSLog(#"superview--%#",[self.view superview]); //it prints superview of your view
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject]; // here you get all subviews on ur touchpoint
if ([touch view] == imageView) {
CGPoint location = [touch locationInView: self.view];
imageView.center = location;
}
}
/// To check both views are in one spot, use this
// CGRectContainsRect() and CGRectIntersectsRect().
You can try this one..
You can get all subviews if you use [self.view subviews]. You can compare those views frames with touch location.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView:self.view];
startX = touchLocation.x;
startY = touchLocation.y;
for (UIView *view in [self.view subviews]){
//Here you can compare each view frame with touch location
}
}

iOS route-me draggable marker looses track (even in samples)

In my iOS app I'm using the route me lib to display a map in offline mode.
I have some markers on the map, and now I would like to make them draggable. I used this code :
- (void) mapView:(RMMapView *)map didDragMarker:(HotspotMarker*)marker withEvent:(UIEvent *)event
{
UITouch* touch = [[event allTouches] anyObject];
if([[event allTouches] count] == 1 && touch.phase == UITouchPhaseMoved)
{
CGPoint position = [touch locationInView:[touch.view superview]];
CGSize delta = CGSizeMake((position.x-(marker.position.x)),
(position.y-(marker.position.y)));
[marker moveBy: delta];
[marker setProjectedLocation:[[_mapView.contents projection]
latLongToPoint:[_mapView pixelToLatLong:marker.position]]];
}
}
The markers drag well when I move the mouse slowly, but when I move the mouse a little quickly, I loose the track.
So I looked the sample "MapTestbedFlipMaps" project and run it... And encountered the same problem.
Then I tried to do it by myself with a gesture recognizer... but I still have the issue.
Any idea ?
EDIT : the problem doesn't come from the marker's image's size, I tried with a 100x100 image and got the same result.
Try This:
- (void) mapView:(RMMapView *)map didDragMarker:(RMMarker *)marker withEvent:(UIEvent *)event
{
CGPoint position = [[[event allTouches] anyObject] locationInView:mapView];
CGRect rect = [marker bounds];
[self.mapView.markerManager moveMarker:marker AtXY:CGPointMake(position.x,position.y +rect.size.height/3)];
}

uiview touch move get the anchorpoint on uiview

I have a view which, when I click on it, will move left and right. I only get the point center of the view. How can I get the uiview on the anchorpoint because where ever i click on the view it will move.
Here is my code:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesMoved called");
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchpoint=[touch locationInView:self.view];
if ([touch view]==secondView)
{
secondView.center=touchpoint;
[self animateFirstPoint:[touch view]];
}
}
If I get you right you're searching for the anchorpoint of the UIView.
First import QuartzCore:
#import <QuartzCore/QuartzCore.h>
Now you can use the anchorpoint of your view:
secondView.layer.anchorPoint = touchpoint;
Remember you have to specify anchor point like CGPointMake(x,y), where x and y are more or equal to 0 or less or equal to 1;
For example:
CGPoint selectionAnchorPoint = CGPointMake(0.15, 0.2);

Resources