Objective c 如何从一个类调用另一个类中的函数

Objective c 如何从一个类调用另一个类中的函数,objective-c,Objective C,我有两个类ThisWillWorkLayer和ThisWillWorkScene ThisWillWorkLayer.m #import "ThisWillWorkLayer.h" #import "ThisWillWorkScene.h" @implementation ThisWillWorkLayer #define kSwipeScale 0.6 -(void) ccTouchMoved: (UITouch *)touch withEvent: (UIEvent *)event

我有两个类ThisWillWorkLayer和ThisWillWorkScene

ThisWillWorkLayer.m

#import "ThisWillWorkLayer.h"
#import "ThisWillWorkScene.h"


@implementation ThisWillWorkLayer


#define kSwipeScale 0.6

-(void) ccTouchMoved: (UITouch *)touch withEvent: (UIEvent *)event {


    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

    NSLog(@"Touch at x:%d y:%d",(int)touchPoint.x,(int)touchPoint.y);

    [ThisWillWorkScene rotate:touchPoint];
}


@end
每次刷卡时都会执行ccTouchMoved功能。我的错误在函数的最后一行<代码>[ThisWillWorkScene旋转:接触点]表示旋转功能不存在。但是,如果您查看ThisWillWorkScene类,它似乎是这样的

这将是一个工作场景

#import "CC3Scene.h"

/** A sample application-specific CC3Scene subclass.*/
@interface ThisWillWorkScene : CC3Scene {
    CGPoint lastPoint;
    CC3Camera* cam;

}

-(void) rotate: (CGPoint) touchPoint;

@end
这将是一个工作场景

@implementation ThisWillWorkScene


#define kSwipeScale 0.6

-(void) rotate: (CGPoint) touchPoint{

    //CC3Camera* cam = self.activeCamera;

    // Get the direction and length of the movement since the last touch move event, in
    // 2D screen coordinates. The 2D rotation axis is perpendicular to this movement.
    CGPoint swipe2d = ccpSub(touchPoint, lastPoint);
    CGPoint axis2d = ccpPerp(swipe2d);

    // Project the 2D axis into a 3D axis by mapping the 2D X & Y screen coords
    // to the camera's rightDirection and upDirection, respectively.
    CC3Vector axis = CC3VectorAdd(CC3VectorScaleUniform(cam.rightDirection, axis2d.x),
                                  CC3VectorScaleUniform(cam.upDirection, axis2d.y));
    GLfloat angle = ccpLength(swipe2d) * kSwipeScale;

    // Rotate the cube under direct finger control, by directly rotating by the angle
    // and axis determined by the swipe. If the die cube is just to be directly controlled
    // by finger movement, and is not to freewheel, this is all we have to do.
    CC3MeshNode* helloTxt = (CC3MeshNode*)[self getNodeNamed: @"Hello"];
    [helloTxt rotateByAngle: angle aroundAxis: axis];

    lastPoint = touchPoint;

}


@end

因此,我的问题是,从ThisWillWorkLayer中调用rotate函数的正确方法是什么?您的
rotate:
方法是实例方法,而不是类方法。要调用实例方法,您需要该对象的一个实例来调用它

例如,此代码适用于您当前的设置:

ThisWillWorkScene *scene = [[ThisWillWorkScene alloc] init]; //Get an instance, you might have to change this
[scene rotate: touchPoint];
如果您希望能够调用对象上的类方法(我不认为这是您想要做的),那么您将进行更改

-(void) rotate: (CGPoint) touchPoint;


该方法被声明为实例方法。您需要在
ThisWillWorkScene
的实例上调用它

如果不需要将其作为实例方法,则将其声明为类方法

//in .h
+(void) rotate: (CGPoint) touchPoint;// Notice the plus

//in .m
+(void) rotate: (CGPoint) touchPoint{
    //Code
}

您的
[ThisWillWorkScene rotate:touchPoint]
正在尝试调用
ThisWillWorkScene
类上的实例方法。要使其工作,您的
rotate:
方法需要使用
+
定义为类方法,如下所示:

+ (void) rotate ...
根据
rotate:
方法的内容,它看起来可能是一个类方法

//in .h
+(void) rotate: (CGPoint) touchPoint;// Notice the plus

//in .m
+(void) rotate: (CGPoint) touchPoint{
    //Code
}
如果您打算使用
ThisWillWorkScene
类的实例,那么它将如下所示:

ThisWillWorkScene *scene = [[ThisWillWorkScene] alloc] init];
[scene rotate:touchPoint];