Question

I'm trying to add an action once an image was tapped. I first tried adding a C4Button and then giving it an image but that didn't work out at all. So now I'm trying to just add a tap function to an image here is the code

-(void)setup{
//IMAGE AS BUTTON
    C4Image *photoButtonImage=[C4Image imageNamed:@"icons-02.png"];
    photoButtonImage.height=NavBarHeight;
    photoButtonImage.center=CGPointMake(self.canvas.width/2, self.canvas.height-NavBarHeight/2);
    [self.canvas addImage:photoButtonImage];
    [photoButtonImage addGesture:TAP name:@"tap" action:@"tapped"];
}
-(void)tapped {
    C4Log(@"tapped!");
}

applying the [photoButtonImage addGesture:TAP name:@"tap" action:@"tapped"]; method to self totally works fine but nothing happens if I add it to the image (PhotoButtonImage). I've looked into 2 answers (Gesture controls crash when calling methods on C4WorkSpace.m and How do I apply a UILongPressGestureRecognizer to a shape in C4?). They are for shapes and I thought it should just work the same for images, but apparently it doesn't...

Was it helpful?

Solution

Adam's answer is correct for gestures. Generally, you want to use them when you want some extra / custom functionality for objects.

However, in your case you want to simply use the image as a button, you can do the following to achieve that without having to use a gesture:

[self listenFor:@"touchesBegan" fromObject:photoImage andRunMethod:@"aMethod"];

The reason for this is that all C4 objects will post basic touch notifications by default.

OTHER TIPS

What is happening is that when you add the gesture to the C4Image you are calling the tapped method of C4Image not the tapped method you have provided in C4Workspace.m. What you need to do is to have the C4Image send a notification when it has been tapped, then have C4Workspace.m listen for the notification and trigger your tapped method.

[photoButtonImage addGesture:TAP name:@"tap" action:@"tapped:"];
[self listenFor:@"tapped" fromObject:photoButtonImage andRunMethod:@"tapped"];

When you add the gesture it is always best to add the colon in order call the method with arguments. This will guarantee that the notifications work as you intend.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top