touchesAnyObject not working as i thought it might - iphone

Im using the following code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.location = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(location));
}
In order to try and find where the user might touch the screen, and i thought that [touches anyobject] would allow it to detect where the user has touched the screen even if another object has been selected. However when I select a button it seems that it is not the case. Could anyone help me out?
Thanks!

UIButtons by default "absorb" the touch, so you have to manually pass it along.
Check out this thread for more info:
Is there a way to pass touches through on the iPhone?

Related

iOS9 upgrade SpriteKit is not displaying correctly

My app was rejected from the appstore with the reason of not performing correctly in iOS9.
I have downloaded iOS9, and it seems that sprite textures are not performing in the order I create them (this worked well in previous versions of iOS).
Also in
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
the well known
for (UITouch *touch in touches) { .. }
loop won't detect the correct nodes.
Any ideas?
I have an issue with SKSpriteNodes and IOS9. The sprites randomly appeared or not, so I have to set in all of them the zPosition property and now I can see all of them.
After testing the bug - it seems that the option of
for (UITouch *touch in touches) { .. }
Indeed no longer identifies all the nodes in touch location, but only the top most node. Which is bad for games which use it. Instead, you need to use only the following method that currently works:
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:location];
for (SKNode *node in nodes) { .. }

draw line using finger and an object will follow that path?

I m new to game devloping in ios. Now I want made a game similar like "Control Air Flight" and "Air Traffic Controller" ,
where a user can draw line using their finger and an object will
follow that path
so ,anyone can guide me which is best for developing like this.Can Cocos2d is best for it? Or any thing else i have to use for this.
Also If anyone knows of a tutorial already out there or any reference link please suggest me.
Thanks in Advance.
To simply let the object follow your finger, implement touches (and just one of its methods):
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint toPoint = [touch locationInView:self.view];
[yourObjectOutlet setCenter:toPoint];
}
Here, your object's center will follow your path, but you can adjust its anchor point by editing "toPoint" accordingly to object's frame.
EDIT
If you want to draw the path, then make the object follow that path, do like this:
//define an NSMutableArray in your header file (do not forget to alloc and init it in viewDidLoad), then:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//you begin a new path, clear the array
[yourPathArray removeAllObjects];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint toPoint = [touch locationInView:self.view];
//now, save each point in order to make the path
[yourPathArray addObject:[NSValue valueWithCGPoint:toPoint]];
}
Now you want to start the move:
- (IBAction)startMoving{
[self goToPointWithIndex:[NSNumber numberWithInt:0]];
}
- (void)goToPointWithIndex:(NSNumber)indexer{
int toIndex = [indexer intValue];
//extract the value from array
CGPoint toPoint = [(NSValue *)[yourPathArray objectAtIndex:toIndex] CGPointValue];
//you will repeat this method so make sure you do not get out of array's bounds
if(indexer < yourPathArray.count){
[yourObject setCenter:toPoint];
toIndex++;
//repeat the method with a new index
//this method will stop repeating as soon as this "if" gets FALSE
[self performSelector:#selector(goToPointWithIndex:) with object:[NSNumber numberWithInt:toIndex] afterDelay:0.2];
}
}
That's all!

Handling touches on Iphone

Im having a little problem on handling touches in my apps.
I set my touchesBegan like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
touchPoint = [currentTouch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchPoint)) {
image1IsTouched = YES;
}
}
Then i set my touch move like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
currentPoint = [currentTouch locationInView:currentTouch.view];
if(image1IsTouched == YES) {
image1.center = CGPointMake(currentPoint.x,currentPoint.y);
.....
}
}
Now i tried my app on actual unit and thats where i notice my problem. While im touching the image1 with 1 finger the app is doing ok and its checking for collision everytime i drag my finger. The problem occurs when i touch the screen with another finger while touching/dragging the image. The image im currently touching will jump to the other finger. I've tried [myView setMultipleTouchEnable:NO]; & using NSArray on touches and comparing the [touches count] with the touch but its not working. Can someone show me how to set a uiimageview to act on single touch only. Thanks.
First, you should use UITouch *currentTouch = [touches anyObject]; to get the current touch.
Second, you should check that touches.count == 1 to make sure there's only one finger on the screen, and ignore touch input if there's more than one, unless you wanted to support multitouch.

Comparing a UITouch location to UIImageView rectangle

I have the following code to determine if a touch is within an image view in my table cell. However, it doesn't work. I compared the two with CGRectContainsPoint however, it doesn't work. Here is the code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Declare the touch and get it's location
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(myImageView.frame, touchLocation))
{
NSLog(#"Tapped image view");
}
}
Thanks for the help!
However, it doesn't work.
Please be more specific.
UITouch *touch = [touches anyObject];
Why not examine every touch, and not simply a random* pick of them?
*The documentation for anyObject says that you are not guaranteed which one it will give you. You are not even guaranteed that it will be random; it could be the same object every time. Murphy's Law says that, whether it is random or not, it will be the wrong one.
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(myImageView.frame, touchLocation))
Your frame is in your superview's co-ordinate system; [touch locationInView:self] returns the touch point in your co-ordinate system. You want to test within bounds, which is in your co-ordinate system. The documentation explains the difference.
The problem is that you need to be calling [touch locationInView:myImageView] to get the point in the image views coordinate system. Then do your check to see if it's within the frame.
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self];
if ([touch view]==view) {
view.center=location;
}
write it in the touch moved event. thanx
Remember that when you're asking a touch for locationInView:, you're getting out a point relative to that view's frame. So, assuming the code snippet you gave was contained in a subclass of UIViewController you should be asking for
CGPoint touchLocation = [touch locationInView:self.view];
Which will give you a point relative to your view. The reason you want a point relative to your current view is because the frame of your image view is also relative to its parent view - the same view. So now it should work.
if (CGRectContainsPoint(myImageView.frame, touchLocation)) {
NSLog(#"Tapped image view");
}

How can I implement a circular scrolling mechanism?

I'm having some images from which the user should choose one. Now I don't want to just offer an flat scrolling area with a boring grid. Instead, I'd like to show up a wheel that contains those images. At the top would be a marker indicating the selection. Something similar to the Pickers.
The problem is not the rotation stuff; I'd use some geometric functions for that. But I have no idea how to actually get the scrolling gestures on that wheel. Where must I start?
BTW: With circular I don't mean something like the Pickers. I mean a real wheel that has a center axis and can be rolled. Like the very old telephones, like a bike wheel. Or a Picker turned by 90°, facing with the axis to you (Z-coordinate).
If you're talking about capturing gestures then here is the example they give in the docs.
Though I could have sworn I heard Alan Cannistraro say in one of the first CS193P lectures that you don't have to do this, that you can just trap the swipe event but I can't find that.
Could someone that actually knows what they are doing please correct me and I'll remove this post but for now I know this will work:
#define HORIZ_SWIPE_DRAG_MIN 12
#define VERT_SWIPE_DRAG_MAX 4
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
startTouchPosition = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self];
// If the swipe tracks correctly.
if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN &&
fabsf(startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX)
{
// It appears to be a swipe.
if (startTouchPosition.x < currentTouchPosition.x)
[self myProcessRightSwipe:touches withEvent:event];
else
[self myProcessLeftSwipe:touches withEvent:event];
}
else
{
// Process a non-swipe event.
}
}
Just how similar to a picker view are you thinking? You can load up a picker view with your own custom subviews, which could be image views. That'd get you an actual picker view with your images, which might or might not be what you're actually aiming for.