Find when a subview is touched when it is animating in iphone? - iphone

I added a subview to the view. I am finding it when touched, when it is animating touch event is finding the subview..
Here is the code...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch self]) {
NSLog(#"View Touched");
}
if ([[touch view] isKindOfClass:[Baloon class]]) {
NSLog(#"Baloon Touched");
}
}

you should implement the "touchesBegan" Method in your Baloon Class and in the "touchesMove" Method set the new Position to the Baloon!
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1: {
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
[self setCenter:[touch locationInView:self.superview]];
}
break;
default:
break;
}
}

Related

Wrong sprite moving when touched

I have 20 sprites in my scene which I've added to a NSMutableArray. My problem is that when I drag one sprite over another, the other also moves. How do I restrict the movement of untouched sprites?
Please help me with code (I am new to Cocos2d).
if( (self=[super init])) {
collection=[[NSMutableArray alloc]init];
CCLayer *base=[CCSprite spriteWithFile:#"Base.png"];
base.position=ccp(512,384);
[self addChild:base];
x=0;
for(int i=1;i<=7;i++)
{
CCSprite *hole=[CCSprite spriteWithFile:#"ball.png"];
hole.position=ccp(140+x,318);
hole.tag=i;
[self addChild:hole];
hole.visible=YES;
[collection addObject:hole];
x=x+75;
}
self.isTouchEnabled=YES;
}
return self;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"count:%i",[collection count]);
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
for(CCSprite *s in collection)
{
if(CGRectContainsPoint([s boundingBox], location))
s.position=ccp(location.x,location.y);
return;
}
}
You can do that:
Declare this in interface .h file
CCSprite *mSpriteOnHand;
CGPoint mLastPos;
Inside init assign it to nil.
mSpriteOnHand = nil;
In touchesBegan method check like this
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for(CCSprite *s in collection)
{
if(CGRectContainsPoint([s boundingBox], location))
{
mLastPos = s.position;
s.position=ccp(location.x,location.y);
mSpriteOnHand = s;
break;
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
if(mSpriteOnHand)
{
mSpriteOnHand.position = location;
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(mSpriteOnHand)
{
mSpriteOnHand.position = mLastPos;
mSpriteOnHand = nil;
}
}

Multi object drag

While dragin multi objects using the following code the the object is shaking when i touch the object,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == img1)
{
CGPoint location = [touch locationInView:touch.view];
img1.center = location;
return;
}
if (touch.view == img2)
{
CGPoint location = [touch locationInView:touch.view];
img2.center = location;
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
What is problem in my code, is there any other method to drag and if the view contain one more object?please help me to solve this issue
I think if you are talking about more object to drag all at once then you should remove the return from you touch begain method

touchesBegan and touchesMoved stuck

i am using this code in my app to recognize touch and drag on specific UIView,
now i have a problem that if i make the touch and immediate make a drag the touchesMoved called only 3-4 times and stop and then the touchesCancelled called, but if i touch the screen wait a second and then make a drag it call touchesMoved every time the finger move.
Edit
Ijust tested it on iphone 4s and with this device it work perfect,in ipod4 it still have the problem. both device are with 5.01 .
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray* touchArray = [touches allObjects];
UITouch* touch = [touchArray objectAtIndex:0];
CGPoint point = [touch locationInView:self.view];
UIView *tmp = [self.view hitTest:point withEvent:event];
if (tmp == volumeViewBackground) {
//do something
}else {
NSLog(#"err");
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray* touchArray = [touches allObjects];
UITouch* touch = [touchArray objectAtIndex:0];
CGPoint point = [touch locationInView:self.view];
UIView *tmp = [self.view hitTest:point withEvent:event];
if (tmp == volumeViewBackground) {
//do something
}else {
NSLog(#"err");
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"error");
}
Try calling super according to UIResponder Class Reference.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan: touches withEvent: event];
NSArray* touchArray = [touches allObjects];
UITouch* touch = [touchArray objectAtIndex:0];
CGPoint point = [touch locationInView:self.view];
UIView *tmp = [self.view hitTest:point withEvent:event];
if (tmp == volumeViewBackground) {
//do something
}else {
NSLog(#"err");
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved: touches withEvent: event];
NSArray* touchArray = [touches allObjects];
UITouch* touch = [touchArray objectAtIndex:0];
CGPoint point = [touch locationInView:self.view];
UIView *tmp = [self.view hitTest:point withEvent:event];
if (tmp == volumeViewBackground) {
//do something
}else {
NSLog(#"err");
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled: touches withEvent: event];
NSLog(#"error");
}

image should follow previous point touches in iphone?

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.

Disable UIView on TouchesMoved

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.