4

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.

4

1 回答 1

4

move:您添加到 Walker 的 PAN 手势的方法实际上是在引用内置于类中的方法(C4Control即所有可视对象)。

要确定手势何时释放,您可以重写该方法以添加一个条件来检查手势的状态何时结束。

为此,您首先将 C4 对象子类化(这里我使用 C4Shape,但您可以使用 C4Image)。

标题将如下所示:

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

该实现将有一个被覆盖的move:方法,如下所示:

#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

...最后,在您的工作区中,您可以像往常一样创建对象并为其添加手势。

#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

您可以从此处获取 C4 项目的副本,该项目显示如何执行此操作:https ://github.com/C4Code/PanGestureEnded


至于您遇到的 C4View 问题,您是对的……目前您不会在 C4Views 上使用手势。您可以将常规 UIGestureRecognizers 添加到 C4Views,但该方法[object addGesture:name:action:]将不起作用,因为我们尚未为 C4View 类实现此功能(但它已经在我们的待办事项列表中)。

于 2012-12-14T08:26:20.537 回答