Drag an image when touched on the object itself - ios

I created a very simple app that contains a draggable UIImageView but I faced a problem that the image can be dragged without touching it. I'm sure that the hole issue is from anyObject but I have no idea how to replace it I tried self.view.image but it failed. So here's my code
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
lineView.center = location;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
any ideas?

Consider using a pan gesture recogniser. Add it only to the image view (and enable user interaction on the view). Now you will only get action method callbacks from the gesture when the view has actually been touched, your code becomes simpler, and you can move the view with that position.
Also, in your current code, it's weird to call touchesBegan: from touchesMoved:. If you keep that code (because you don't want to use a gesture), you should refactor the code that moves the view out into a different method and have both touchesBegan: and touchesMoved: call that method.

Try the following
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if ([lineView pointInside:location withEvent:event])
lineView.center = location;
}

Related

Dragable UIImageView is Teleporting when tapping somewhere else

The code below is based on the idea of how to make a UIImageView be able to be dragged around the ViewController. When I use this code however, I click on a different location instead of pressing on the Image and it teleports to that location instead of requiring me to always drag the image. I want the code below to only work when pressing on that specific image. Please Help-
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
image.center = location;
[self ifCollided];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
It's teleporting simply because you don't have any check for whether the user has in fact touched the image, so any touch causes the image to jump to that location. What you need to do is check if the user has touched the image, then move it only if they have. Try the following code in the ViewController, assuming 'image' is the draggable view:
You'll need to create a variable/property on your ViewController to track if the view is being dragged. If you need just one image, you can use a BOOL (the code below assumes you've done this, called isDragging).
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:image];
if([image pointInside:location withEvent:event]) {
isDragging = YES;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if(isDragging)
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
image.center = location;
[self ifCollided];
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
isDragging = NO;
}
This code basically does a check in touchesBegan and sets the property to true if you touch inside the image, moves it in touchesMoved if your initial touch was on the image, and finally unsets the check in touchesEnded.

Drawing on Ipad [duplicate]

Is there any function or algorithm to catch all the points when drawing in iPhone? Like in Android, where we have gethistoical points.
Check the methods touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: and touchesCancelled:withEvent: to detect movements
For example using the method -touchesMoved:withEvent:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(currentPosition));
}
For more info check the documentation of the UIResponder class

Get historcial points ios

Is there any function or algorithm to catch all the points when drawing in iPhone? Like in Android, where we have gethistoical points.
Check the methods touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: and touchesCancelled:withEvent: to detect movements
For example using the method -touchesMoved:withEvent:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(currentPosition));
}
For more info check the documentation of the UIResponder class

Move a UIView with my finger

I'm trying to move a UIView (let's call it viewA), which is located on top of another UIView (let's call it viewB). viewB has the size of the iPad screen, and view A is much much smaller.
I manage to move things, but the whole screen moves (viewA + viewB), not only viewA.
Here is my code for viewA class:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self->view];
if (CGRectContainsPoint(self.window.frame, touchLocation)) {
dragging = YES;
oldY = touchLocation.y;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self->view];
if (dragging) {
CGRect frame = self.window.frame;
frame.origin.y = self.window.frame.origin.y + touchLocation.y - oldY;
self.window.frame = frame;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
}
What I do not understand, is that, I implemented these methods in the viewA class. "Self" should concern this viewA, right? So why does the whole page move when my finger move on the screen?
You are moving the entire window, which contains all the views, you should only move the view you want, viewA in your case.

how to move the UIButton

i create a UIButton *actionbtn,it's default image is 1.png,and hightlight image is 2.png, i select this button,and move it to any place of the screen.my code is
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self];
actionbtn.frame = CGRectMake(touchMoved.x-40, touchMoved.y-40, 80, 80);
}
if i clicked the button and move it,it can not be moved,but if i touch the screen,and move on the screen the button can work...
Try this:
http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007315
It's an example with an UIButton that move on screen...
The button is moving when you keep touching the screen because you implemented -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;. If you attempted to move the button when simply touching the screen anywhere, place that code in -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;.
add an if clause in touchesMoved function.
UITouch *touch = [touches anyObject];
if( touch.view == button){
[button.center = [touch locationInView:self.view];
}

Resources