Detecting touches in a triangular grid of buttons? - ios

In the photo below, each triangle is a separate subclassed SKShapeNode. How do you recognize which triangle is touched? Currently in the scene's toucheBegan: method, touching the grid detects two triangles since their frame's are square.

I managed to solve this problem by setting the UIBezierPath path that draws the triangle as a property on the Triangle class. In the scene's toucheBegan: method, I check if the touch is contained within the Triangle's touchableArea UIBezierPath property.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint position = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:position];
for (TrianglePiece *triangle in nodes) {
if ([triangle isKindOfClass:[TrianglePiece class]]) {
position = [touch locationInNode:triangle];
if ([triangle.touchableArea containsPoint:position]) {
// Perform logic here.
}
}
}
}

Related

pointInside:withEvent inside uibezierpath (overlapping area between two views)

no matter how much I tried I couldn't have access (Handle scrolling/Touchevents) to the overlapping area (right triangle) between two square shaped views A and B (A in on top of B)as shown in this image
I want the right part (triangle B) with is defined by UIbezierpath to handle the scrolling for the view beneath it(which is B). I couldn't have access to it by pointInside: withEvent: since its a bezierpath.
Even touchesBegan:withEvent: didn't work at all
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([leftPath containsPoint:touchPoint])
{ //Do something
}
}
Please help.
You have to implement hitTest method of UIView to detect touch on particular view. Simply subclass your View and implement hitTest method.
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if ([path containsPoint:point]){
return [super hitTest:point withEvent:event];
}
else{
return nil;
}
}

Set touchable area of SKSpriteNode

Is there a property of SKSpriteNode that lets you manually set its touchable area?
I have a sprite textured with a PNG, it only seems to detect touches on the opaque portion of the PNG. So a small circle inside a large blank canvas actually has a tiny touchable area.
Create an SKNode that is the size you would like the touchable area to be. Add your textured sprite as a child of the new SKNode. Check to see if the new SKNode was touched instead of if the textured sprite was touched.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"childName"])
{
NSLog(#"You touched to child of sprite");
}
}
-(void)sprite
{
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:#"spriteImageName"];
[self addChild:sprite];
SKSpriteNode *spriteChild = [SKSpriteNode spriteNodeWithImageNamed:#"childImageName"];
spriteChild.name = #"childName";
[sprite addChild:spriteChild];
}

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...
}

Find out, that the touch position is in Imageview

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.

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