I'm converting the game I'm working on from UIkit to coocos2d. Using UIKIT I would use the code below code to pass the touch evens to a method. What would be the equivalent in Cocos2d?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Save the position
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil];
}
}
// Saves the first position for reference when the user lets go.
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
beginTouchPoint = touchPoint;
}
// Handles the continuation of a touch.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]];
}
}
-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{ id sender;
int directionSwiped;
int row,column;
CGFloat xDelta = position.x - beginTouchPoint.x;
CGFloat yDelta = position.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
What would be the equivalent using cocos2d?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Save the position
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil];
}
}
// Saves the first position for reference when the user lets go.
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
beginTouchPoint = touchPoint;
}
// Handles the continuation of a touch.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]];
}
}
-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{ id sender;
int directionSwiped;
int row,column;
CGFloat xDelta = position.x - beginTouchPoint.x;
CGFloat yDelta = position.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
I've tried to figure this out on my own, and I've spent hours on google, but I haven't come up with a workable solution.
I figured it out. I discovered that, what I was forgetting to do, was to add:
self.isTouchEnabled = YES;
to the Layer's init method.
After I did that the following code worked for me (beginTouchPoint and endTouchPoint are properties of the class):
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
beginTouchPoint = [[CCDirector sharedDirector]convertToGL:location];
}
// Handles the continuation of a touch.*
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
static BOOL isFirstTouch = YES;
UITouch* myTouch = [touches anyObject];
int row,column;
int directionSwiped;
CGPoint location = [myTouch locationInView: [myTouch view]];
endTouchPoint = [[CCDirector sharedDirector]convertToGL:location];
CGFloat xDelta = endTouchPoint.x - beginTouchPoint.x;
CGFloat yDelta = endTouchPoint.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
Related
When touch began-it shows the image,when touchmoved the image follows the touch events, when touch ended the image disappear
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
newgicg.hidden = NO;
NSArray *allTouches = [touches allObjects];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
int count = [allTouches count];
if (count == 1) {
newgicg.center = touchLocation;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
newgicg.center = touchLocation;
[self.view addSubview:newgicg];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
newgicg.hidden = YES;
}
I want something like ,to follow the collection of stars when the cursor moved on the screen.i want the stars to be blinks and vanished wherever the cursor goes on the screen
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"simple-apple.png"]];
imageView.center = touchLocation;
[self.view addSubview:imageView];
[imageView release];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
try this code instead, just replace the name of the image.
EDIT:
one thing I forgot to mention is that you can keep a counter to the no. of subviews added this way and remove them , otherwise it may also remove other views which were there on the view before.
I have an application in which i have to move images, problem is arising when mouse touches the images it gets moved but if it touches the main View, whole view is also moving.
I tried view.userInteractionEnabled=NO;
but after one move whole view gets freeze.
I want my view to be static(not moving)
Help !!!
here is the code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
UIImageView *imgPimples = [[UIImageView alloc]init];
imgPimples = (UIImageView*)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimples];
imgPimples.center = touchLocation;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
Just check whether touched class is of kind UIImageView.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]]) {
UIImageView *imgPimple;
imgPimple = (UIImageView *)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimple];
imgPimple.center = touchLocation;
}
}
I think it will serve your purpose.
if ([touch.view isKindOfClass:[UIImageView class]]){
imgPimples.center = touchLocation;
}
assuming your parent view is not also a UIImageView.
Is there a way to tell if the touch is currently moving? If so could someone lead me to the right place?
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
//Here urSprite is your CCSprite which you want to move
CGRect urSpriteRect = CGRectMake([urSprite position].x - [urSprite contentSize].width/2,
[urSprite position].y - [urSprite contentSize].height/2,
[urSprite contentSize].height, [urSprite contentSize].width);
if (CGRectContainsPoint(urSpriteRect, location))
{
checkFlag = TRUE;
NSLog(#"You Touched the sprite!");
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(checkFlag){
urSprite.position = location;
NSLog(#"You are moving your sprite!");
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
checkFlag = FALSE;
NSLog(#"Sprite stopped!");
}
If your layer has touches enabled, you can implement the following method in your layer to handle move events for touches...
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
I have viewcontrollers set up to track gestures and commit an action, in this case, change the tab. The code for the viewcontrollers is as follows:
#define HORIZ_SWIPE_DRAG_MIN 100
CGPoint mystartTouchPosition;
BOOL isProcessingListMove;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint newTouchPosition = [touch locationInView:self.view];
if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
isProcessingListMove = NO;
}
mystartTouchPosition = [touch locationInView:self.view];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint currentTouchPosition = [touch locationInView:self.view];
// If the swipe tracks correctly.
double diffx = mystartTouchPosition.x - currentTouchPosition.x + 0.1; // adding 0.1 to avoid division by zero
double diffy = mystartTouchPosition.y - currentTouchPosition.y + 0.1; // adding 0.1 to avoid division by zero
if(abs(diffx / diffy) > 2.5 && abs(diffx) > HORIZ_SWIPE_DRAG_MIN)
{
// It appears to be a swipe.
if(isProcessingListMove) {
// ignore move, we're currently processing the swipe
return;
}
if (mystartTouchPosition.x < currentTouchPosition.x) {
isProcessingListMove = YES;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
return;
}
else {
isProcessingListMove = YES;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2];
return;
}
}
else if(abs(diffy / diffx) > 1)
{
isProcessingListMove = YES;
[super touchesMoved:touches withEvent:event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
isProcessingListMove = NO;
[super touchesEnded:touches withEvent:event];
}
I'd like to have the same gesture do the same thing on a uiwebview I have set up under the view of the viewcontroller. In short, the uiwebview is on the view. When I swipe, I just get the basic uiwebview functions (scrolling). If I set up a custom uiwebview class with the same code as the uiviewcontroller, I get errors. I need to know how to translate that block of code, keeping the functionality intact, to suit the uiwebview.
You should be able to subclass UIWebView and override those methods. You may have to override all of the touch methods, though,
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
EDIT:
Ah, I see your errors now. They are because those things don't apply to the UIWebView like they did to the parent (which you already know...)
Since UIWebView is a UIView, you should be able to replace
[touch locationInView:self.view]
with
[touch locationInView:self]
To get to the parent's tab bar controller, though, you'll have to design a different way to do it. You could pass a reference to the parent to your UIWebView subclass and expose a method on the parent for changing which tab is active, for instance:
In your subclass, add a property for the parent tab bar controller:
UITabBarController *parent;
When creating your UIWebView subclass and adding it to the tab bar controller, also set the parent:
webView.parent = self;
If you're setting this up in Interface Builder, make the parent an IBOutlet and hook it up through IB.
In your tab bar controller view, add a method that changes the selected controller. You could use named constants for the view you want to switch to, or methods that describe exactly what you are switching to, but for simplicity, you'd do something conceptually similar this:
- (void) switchToView: (int)viewNumber
{
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:viewNumber];
}
then call [parent switchToView:2] in your code.
Another and arguably better way is to use NSNotifications, registering the parent as a listener.
My full code is the following:
#import "CustomWebView.h"
#implementation CustomWebView
//Swipe between tabs
#define HORIZ_SWIPE_DRAG_MIN 100
CGPoint mystartTouchPosition;
BOOL isProcessingListMove;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint newTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"
if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
isProcessingListMove = NO;
}
mystartTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint currentTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"
// If the swipe tracks correctly.
double diffx = mystartTouchPosition.x - currentTouchPosition.x + 0.1; // adding 0.1 to avoid division by zero
double diffy = mystartTouchPosition.y - currentTouchPosition.y + 0.1; // adding 0.1 to avoid division by zero
if(abs(diffx / diffy) > 2.5 && abs(diffx) > HORIZ_SWIPE_DRAG_MIN)
{
// It appears to be a swipe.
if(isProcessingListMove) {
// ignore move, we're currently processing the swipe
return;
}
if (mystartTouchPosition.x < currentTouchPosition.x) {
isProcessingListMove = YES;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0]; "error: request for member 'tabBarController' in something not a structure or union"
return;
}
else {
isProcessingListMove = YES;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2]; "error: request for member 'tabBarController' in something not a structure or union"
return;
}
}
else if(abs(diffy / diffx) > 1)
{
isProcessingListMove = YES;
[super touchesMoved:touches withEvent:event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
isProcessingListMove = NO;
[super touchesEnded:touches withEvent:event];
}
// End of swipe
#end
The errors are as follows are included with the code.
I appreciate the help.
Has anyone been able to implement a successful swipe gesture on a UITableView? By default this is not possible due to the scrolling nature of the control. I've tried subclassing UITextView and implementing the swipe function in the instance methods, but no dice.
My UITextView has scrolling disabled - Unless there is another way to implement multiline text?
[Edit] What I should say is input multiline text[/Edit]
Here is a subclass of a UITextView that will detect a swipes gesture...
Is this what you are looking for?
#import <UIKit/UIKit.h>
#define kMinimumGestureLength 25
#define kMaximumVariance 5
#interface SwipeableTextView : UITextView {
CGPoint gestureStartPoint;
}
#end
#implementation SwipeableTextView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch =[touches anyObject];
gestureStartPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
NSLog(#"Horizontal swipe detected");
}
}
#end
An even better solution, I have found, is to simply pass touches back to the superview:
#interface SwipeableTextView : UITextView {
}
#end
#implementation SwipeableTextView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self.superview touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.superview touchesEnded:touches withEvent:event];
}
#end
Add a transparent UIView on top of the UITextView and use it to handle the swipe, and send the touchesBegan/Moved/Ended/Canceled: messages to the text view to preserve normal interaction.
I took Ross's code and added a few things that helped me. In particular, this code won't respond to the swipe until it stops. It also prevents a swipe from reversing direction partway through.
#import <UIKit/UIKit.h>
#define kMinimumGestureLength 25
#define kMaximumVariance 5
typedef enum swipeDirection {
kSwipeNone,
kSwipeLeft,
kSwipeRight
} tSwipeDirection;
#interface SwipeableTextView : UITextView {
CGPoint gestureStartPoint;
tSwipeDirection swipeDirection;
}
#end
#implementation SwipeableTextView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
swipeDirection = kSwipeNone;
UITouch *touch =[touches anyObject];
gestureStartPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
// Check if we already started a swipe in a particular direction
// Don't let the user reverse once they get going
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance &&
swipeDirection == kSwipeNone) {
if (gestureStartPoint.x < currentPosition.x) {
swipeDirection = kSwipeRight;
}
else {
swipeDirection = kSwipeLeft;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (swipeDirection == kSwipeRight) {
NSLog(#"Swipe right");
}
else if (swipeDirection == kSwipeLeft) {
NSLog(#"Swipe left");
}
[super touchesEnded:touches withEvent:event];
}
#end
Usually you implement the touch responding events in a UITableViewCell, which can recognize the swipe and do something. UITableViews inherit from UIScrollView which does not like having touch events overridden.
You could also use a two finger swipe. When I get home I can elaborate with some code but that was what I have used