PAN gesture ending not triggering 'touches ended' with a C4View Subclass? (C4)

StackOverflow https://stackoverflow.com/questions/13869863

  •  07-12-2021
  •  | 
  •  

Question

So I'm using the C4 coding framework for IOS and I've encountered something I can't quite figure out.

I'm using two examples for the C4 github account together - the testScrollView and WalkCycle examples.

I have a C4View subclass called Walker which uses the WalkCycle example as a the template for a small animated character. This character is placed into a scrollable view created from the testScrollView example. He's got a pan gesture added so that the user can drag him to the top of the screen, and watch him fall and splat on the bottom.

Only problem is I can't get any kind of method called when the pan gesture is ended, not from the -(void)touchesEnded command and not from others I've tried.

Some links to similar problems that haven't helped me but might help someone else work this out:

My totally uneducated and baseless hunch is that it has something to do with the walker being a child of C4View, as I didn't have this problem recognizing touchesEnded with c4shapes and images.

I'm not really sure what to post for code, but here are the relevant bits from Walker.m;

-(void)setup {
walkA = [C4Image animatedImageWithNames:@[@"beaviswalk1.tiff",
         @"beaviswalk2.tiff",
         @"beaviswalk3.tiff",
         @"beaviswalk4.tiff",
         @"beaviswalk5.tiff",
         @"beaviswalk6.tiff",
         @"beaviswalk7.tiff",
         @"beaviswalk8.tiff",
         @"beaviswalk9.tiff",
         ]];
[self addImage:walkA];
currentImage = walkA;
[walkA play];

mover = [C4Timer automaticTimerWithInterval:0.05f target:self method:@"walking" repeats:YES];


[self addGesture:TAP name:@"tapGesture" action:@"trip"];
[self addGesture:PAN name:@"panGesture" action:@"move:"];}

and touchesEnded:

-(void)touchesEnded {
C4Log(@"touches Ended");
}

and looking at Walker.h I just realized something strange;

#import "C4View.h"

@interface Walker : C4Image
-(id)initWithFrame:(CGRect)frame;
-(void)play;
-(void)switchWalkCycle;
-(void)trip;
-(void)bleed;
-(void)walking;
-(void)touchesEnded;
-(void)noTouching;
@end

I changed the @interface to C4Image at some point, at the time it seemed to effect nothing and allowed me to have TAP and PAN gestures that worked. Until now....Changing it back to a C4View eliminates support for all gestures. I imagine the problem is there?

Apologies if there is a simple solution but I'm quite lost in trying to fix this.

Was it helpful?

Solution

The move: method that you're adding to your Walker's PAN gesture is actually referencing a method built into the C4Control class (i.e. all visual objects).

To figure out when the gesture is released, you can override the method to add a condition that checks for when the gesture's state has ended.

To do this, you first subclass a C4 object (here I'm using C4Shape, but you can use C4Image).

The header will look like this:

#import "C4Shape.h"
@interface MyShape : C4Shape
@end

The implementation will have an overridden move: method, like this:

#import "MyShape.h"
@implementation MyShape

-(void)move:(id)sender {
    [super move:sender];
    UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *)sender;
    if(panGesture.state == UIGestureRecognizerStateEnded) {
        [self methodToRunWhenPanGestureEnds];
    }
}

-(void)methodToRunWhenPanGestureEnds {
    C4Log(@"the pan gesture ended");
}

@end

... finally, in your workspace you can create a the object and add a gesture to it like you would normally.

#import "C4WorkSpace.h"
#import "MyShape.h"
@implementation C4WorkSpace
-(void)setup {
    MyShape *s = [[MyShape alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [s rect:s.frame];
    [s addGesture:PAN name:@"pan" action:@"move:"];
    [self.canvas addShape:s];
}
@end

You can grab a copy of a C4 project that shows how to do this from here: https://github.com/C4Code/PanGestureEnded


As for the C4View issue you were having, you're right... You won't get gestures working on C4Views at the moment. You can add regular UIGestureRecognizers to C4Views, but the method [object addGesture:name:action:] won't work because we haven't yet implemented this for the C4View class (but it's on our todo list already).

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