Have an image follow your finger along the x axis, but have a fixed y-coordinate - ios

I'm using the touchesMoved method to have an image "follow" someone's finger along the bottom of the screen. So the image will follow the x position of the finger, but it ignores the y position and stays vertically-fixed at the bottom (but not horizontally-fixed). Is there some way I can implement this?
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// create basket image that will be shown on the screen
basketView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"bucket.png"]];
basketView.frame = CGRectMake(130.0, 412.0, 50.0, 50.0);
[self.view addSubview:basketView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get current touch location
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
// update location of the image
basketView.center = point;
}

Maintain the y position, change the x
basketView.center = CGPointMake(point.x, basketView.center.y);

In touchesMoved Do this
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get current touch location
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
// update location of the image
basketView.center = CGPointMake(point.x,basketView.center.y);
}

Do as follows...
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get current touch location
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
point.y=basketView.center.y; //Fix 'y' of basket
// update location of the image
basketView.center = point;
}

Related

ios locationInView not show correct log when I click direct view

I feel strange, I have dark view(darkScreenView) and green view.
I using the
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}
When I click on the dark view or green view,
The Log still show 1. why?
I click on the green view should show log 0, but I still get the log result 1.
I am using xcode 6.3.1.
Have anyone know what problem in this situation?
Thank you~
This will work
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
CGPoint pointOfDarkView = [touch.view convertPoint:touchViewPoint toView:self.darkview];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkview pointInside:pointOfDarkView withEvent:event]);
Your problem:
touchViewPoint is relative to touch.view coordinate
You have to convert point to darkview coordinate first,then you can use pointInside:pointOfDarkView
Try to get touch for different views..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.view==GREEN_VIEW)
{
//Write your code for green view here
}
else if(touch.view==DARK_VIEW)
{
//Write your code for dark view here
}
}
Your touchViewPoint's location is relative to the touched view. So get location relative to self.darkView like below.
CGPoint touchViewPoint = [touch locationInView: self.darkView];
Here is the problem change to View you are referring to.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:darkView]; <----- Problem
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}

How to make create image when screen is touched then be able to move it

I am trying to create a dot on the screen when it is touched and then be able to move that dot around and then ending the touch which will bring up an alert view. For the first touch and movement i have this code so far...
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
dot.image=[UIImage imageNamed:#"hockey-puck-stress-toy-superextralarge-175648.png"];
[self.view addSubview:dot];
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
dot.center = location;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}
The problem is that this creates a new object when moving instead of just when I first touch it. I understand why it does this, but i don't know any way to fix it. Could someone please help? Thanks!
Create a property in your .h file:
#property (nonatomic) UIImageView *dot;
And then just create the view if it doesn't exist and then move the property:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.dot) {
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
dot.image=[UIImage imageNamed:#"hockey-puck-stress-toy-superextralarge-175648.png"];
[self.view addSubview:dot];
}
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
self.dot.center = location;
}

How to detect touches in physicsBody area?

I did create SKSpriteNode using this code and physicsBody is circle. How I could check that user did touch eare in the physicsBody?
self = [super initWithColor:[SKColor clearColor] size:CGSizeMake(200, 200)];
//...
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2.0f];
self.userInteractionEnabled = YES;
I did found solution for my problem using this code:
CGMutablePathRef pathRef;
// Fill pathRef with points and create phisycs body using pathRef.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if (CGPathContainsPoint(pathRef, nil, touchLocation, NO)) {
// If we are here it means user did touche physic body.
}
}
The easiest solution would be to create a second invisible sprite, with the size you need, and place it directly over the area you want to record touches on.
The other option is to store ALL the coordinates which trigger a valid touch and compare them against the actual touch coordinate in your touch method, like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
NSLog(#"%#", NSStringFromCGPoint(touchLocation));
// Compare the touchLocation coordinate against your list of acceptable coordinates...
}

move Sprite with touches moved

i am moving a sprite using the touches moved method. currently the sprite jumps to the point on which the screen is touched but I want the sprite only to move when it is touched directly.
my code:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint newPosition = CGPointMake(location.x, self.size.height/2);
self.sprite.position = newPosition;
}
}
check if the touch location is inside the sprite, like this:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
if(CGRectContainsPoint(self.sprite.boundingBox,positionInScene)) {
CGPoint newPosition = CGPointMake(positionInScene.x, self.size.height/2);
self.sprite.position = newPosition;
}
}

how to make an object draggable?

hey guys i was wondering if there was away to make an object like a UIImageView draggable. I am aware of the UITouch and touches i.e.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *drag=[[event allTouches] anyObject];
player.center=[drag locationInView:self.view];
}
but in this way it is possible for the user to make the object jump across the screen to where he or she touches the screen but i want the user to have to manually drag the object across the screen.
please explain with code and let me know if i have to be more specific or clear...
You can do this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.view];
if(CGRectContainsPoint(self.playerView.frame,location)) {
[UIView beginAnimations:#"Dragging A DraggableView" context:nil];
self.playerView.center = location;
[UIView commitAnimations];
}
}
This should give you a smooth animation.
if you want your user to drag&drop, first you have to check if the touch is inside the imageview rect frame. To improve the user experience, you can detect the touch offset from the imageview center; a good place to do this is inside the touchesBegan:withEvent:. After that, you have to change the position of the imageview, like this:
CGPoint touchOffset; //insert this inside your interface private iVars
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *drag=[[event allTouches] anyObject];
CGPoint touchLocation = [drag locationInView:self.view];
touchOffset = CGPointMake(player.center.x-touchLocation.x,player.center.y-touchLocation.y)
if(CGRectContainsPoint(player.frame,touchLocation)
player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *drag=[[event allTouches] anyObject];
CGPoint touchLocation = [drag locationInView:self.view];
if(CGRectContainsPoint(player.frame,touchLocation)
player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y);
}
P.S. - Next time try to search similar questions...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* aTouch = [touches anyObject];
UIView* aView = [aTouch view];
if (aView != self.view) {
[self.view bringSubviewToFront:aView];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* aTouch = [touches anyObject];
UIView* aView = [aTouch view];
if (aView != self.view) {
aView.center = [aTouch locationInView:self.view];
}
}

Resources