I have a classic SKScene with some buttons (all made programmatically) and ViewController for that scene. Where should be touches events handled - in SKScene, or in ViewController. I need to switch to another scenes and another view controllers when touching different buttons via push segue. When I handle touches events in view controller, it returns me nil for SKNode touched. Here is my code in view controller (scene is its property):
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self.scene];
SKNode *node = [self.scene nodeAtPoint:location];
if ([node.name isEqual: #"campaign"]) {
CampaignViewController *levelViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CampaignScene"];
[self.navigationController pushViewController:levelViewController animated:NO];
}
}
Thank you for explaining.
Implementing touch delegates in the ViewController cannot possibly get you the nodes, as it is the SKScene which manages them. Hence, to be able to use nodeAtPoint:, you need to implement the touch delegates in the SKScene itself.
Now, you also need a way for the SKScene to communicate with the UIViewController, and pass messages which will trigger segues or other methods. For this, you can either use Delegation or NSNotificationCenter, the implementation of which is demonstrated in this answer.
After you have implemented either of the options from the answer, your code should look like the following:
//In ViewController.m
-(void) presentCampaignVieController
{
CampaignViewController *levelViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CampaignScene"];
[self.navigationController pushViewController:levelViewController animated:NO];
}
//In SKScene.m (Using Delegation)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self.scene];
SKNode *node = [self.scene nodeAtPoint:location];
if ([node.name isEqual: #"campaign"]) {
[self.delegate presentCampaignVieController];
}
}
In order to call the same method in the viewController using NSNotificationCenter, you will first have to add an observer:
//ViewController.m, under viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(presentCampaignVieController) name:#"gotoCampaign" object:nil];
//In SKScene.m (Using NSNotificationCenter)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self.scene];
SKNode *node = [self.scene nodeAtPoint:location];
if ([node.name isEqual: #"campaign"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"gotoCampaign" object:nil];
}
}
Related
I cannot figure out what is wrong. Below is my code, and it calls the delegate methods once then stops.
What should I do? I haven;t been able to find the sample code that uses these delegate methods. All I've found were gesture recognizers for swipes and taps, using different delegates.
Code so far:
-(void)initTouchesRecognizer{
DLog(#"");
recognizer = [[UIGestureRecognizer alloc] init];
[self addGestureRecognizer:recognizer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
DLog(#"");
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
{
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
DLog(#"");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
I call initTouchesRecognizer from initwithrect for my image view.
What am i doing fundamentally wrong?
UIGestureRecognizer is an abstract class, you're not supposed to add it directly to your view. You need to use a concrete subclass that inherits from UIGestureRecognizer, like UITapGestureRecognizer or UIPanGestureRecognizer for example. You could also make your own concrete subclass but that usually isn't necessary.
Here is an example of adding a UIPanGestureRecognizer to your view (in your view class code, often the gesture is added to the view from the controller):
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(mySelector:)];
[self addGestureRecognizer:panGesture];
In this case, the selector will be called when ever the user pans in this view. If you added a UITapGestureRecognizer, the selector would be called when the user tapped.
You can check out the apple docs for more info:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW2
Also, I find Paul Hagerty's Stanford lectures to be great, here's one on gesture recognizers:
https://itunes.apple.com/ca/course/6.-views-gestures-january/id593208016?i=132123597&mt=2
You should also understand that none of the methods that you posted are delegate methods, and none of them have anything to do with the UIGestureRecognizer that you added in your code. Those are instance methods of UIResponder (a class that UIView inherits from) that you're overriding. The abstract UIGestureRecognizer also has instance methods with those same names, but it is not the UIGestureRecognizer methods that are getting called in your class.
There was no need to add the gesture recognizer. By overriding the touchesMoved, touchesEnded and touchesBegan methods, I was able to track the user's finger across the screen.
simply do not call the:
-(void)initTouchesRecognizer
code, and the code I originally posted will work.
I have a UIViewController that contains a UIScrollView, which has a UIView inside of its contentview.
I have the following code that does not work, keyboard is not dismissed, why?:
#pragma mark -
#pragma mark Touch Events
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == scrollView || [touch view] == self.view)
{
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
}
touchesBegan:withEvent: is a UIView method, not a UIViewController method. What are you trying to achieve here? You should very seldom have a UI reaction to touchesBegan:. You probably mean to use UITapGestureRecognizer instead.
Make sure to use accessors (self.scrollView) rather than accessing your ivars directly. Direct ivar access is the #1 cause of memory management crashes.
I'm posting this message because I've been reading the forum and I haven't been able to find a similar problem. I need to be able to discriminate taps and double taps (this is a standard thing) BUT my problem is that for whatever reasons I have a Scroll View inside another ScrollView. So, I had to sub-class my ScrollView in order to get touchedBegin method called.
I have a class called PhotoViewController (a sub-class of BaseViewController) this class contains another class called CustomScrollView (a subclass of ScrollView). I needed to sub-class this CustomScrollView from ScrollView in order to override the touchesBegin method, and to be able to capture the touches made by the user.
I tried calling the touchesBegin method from CustomScrollView using something like return [super touchesBegan:touches withEvent:event] inside the touchesBegin method, but when the touchesBegin method inside PhotoViewController gets called it's parameters are empty (and I can't discriminate if the user made a single or double tap, which is exactly what I need)
I have a class, called PhotoViewController:
#class PhotoViewController
#interface PhotoViewController : BaseViewController <UIScrollViewDelegate> {
CustomScrollView* myScrollView;
}
#implementation PhotoViewController
...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
switch (tapCount) {
case 1:
[self performSelector:#selector(singleTapMethod) withObject:nil afterDelay:.4];
break;
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTapMethod) object:nil];
[self performSelector:#selector(doubleTapMethod) withObject:nil afterDelay:.4];
break;
default:
break;
}
}
the class CustomScrollView is (CustomScrollView.h):
#interface CustomScrollViewPhoto : UIScrollView {
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
#end
and it's implementation is this(CustomScrollView.m):
#implementation CustomScrollViewPhoto
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview touchesBegan:[NSSet set] withEvent:event];
return [super touchesBegan:touches withEvent:event];
}
Am I going in the wrong direction with what I want to do? Maybe, I should capture the taps/double taps inside the CustomScrollView class(this works fine!), and from there using a #selector or something call the appropiate methods in PhotoViewController?
Thanks for reading!
I think you're going the wrong route (only slightly!). I do a very similar thing in a photo viewer and I capture the touches in the CustomScrollView. You shouldn't need to do anything in PhotoViewController.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
if(touch.tapCount == 2)
{
if (self.zoomScale == self.minimumZoomScale)
{
//Zoom where the user has clicked from
CGPoint pos = [touch locationInView:self];
[self zoomToRect:CGRectMake((pos.x - 5.0)/self.zoomScale, (pos.y-5.0)/self.zoomScale, 10.0, 10.0) animated:YES];
}
else
{
//Zoom back out to full size
[self setZoomScale:self.minimumZoomScale animated:YES];
}
}
}
I have a UIButton which sends the draginside event to:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
It does this through the following code in viewDidLoad:
[colourButton1 addTarget:self action:#selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
Within the method it sends it to, there is the following line:
UITouch *myTouch = [touches anyObject];
And for some reason, when dragging inside the UIButton this crashes the app. Any ideas why?
EDIT: The solution..
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
When you're adding target to control you can pass 3 types of selectors for action -
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event
Names don't matter, it's number of parameter which is important. If control sends it's target a message with 2 params then first parameter will be control itself (instance of UIButton in your case), second one - instance of UIEvent. But you expects instance of NSSet as first parameter and send it a message anyObject which UIButton doesn't understand. This is the cause of crash.
Why are you trying to send event from UI control to touch handling method touchesMoved:withEvent: in the first place? It will probably do something different from what you meant.
UPDATE:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint touchLocation = [t locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(touchLocation));
}
- (IBAction)buttongDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:button];
[self touchesMoved:touches withEvent:event];
}
Notice that since touchesMoved:withEvent: is a UIResponder's method and controller's view is UIResponder this method will be also called on touch events for this view.
HI . i am trying to move an UIImageView object with UITouch and have problem with the code how can i implement UITouch to detect only my UIImageView object ?
**imageA it my UIImageView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *toucheA = [[event allTouches] anyObject];
if ([toucheA view] == imageA) {
CGPoint location = [toucheA locationInView:self];
imageA.center = location;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
CGPoint postion = imageA.center;
imageA.center = postion;
postion.y = 230;
imageA.center = postion;
}
but doesn't work !
I think you can make a custom UIImageView by subclassing and receive touch event you want inside the custom view.
Generally, the top most view will receiver the touch event. If the view process the touch event and doesn't pass the event to another view, then the others view behind it are impossible to receive any touch event. If the view cannot deal with the touch event, it will pass that by calling super's method.