给Cocos2D视图添加手势支持

前端之家收集整理的这篇文章主要介绍了给Cocos2D视图添加手势支持前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

见如下代码

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self
                                                                        action:@selector(swipeLeft)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [[[CCDirector sharedDirector] view] addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self
                                                                        action:@selector(swipeRight)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [[[CCDirector sharedDirector] view]addGestureRecognizer:swipeRight];

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self
                                                                        action:@selector(swipeUp)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [[[CCDirector sharedDirector] view]addGestureRecognizer:swipeUp];

    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self
                                                                        action:@selector(swipeDown)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [[[CCDirector sharedDirector] view]addGestureRecognizer:swipeDown];

以上代码添加4个监听器(listeners)监视滑向4个不同方向。

手势识别器需要添加到UIView上。而Cocos2D应用程序的主UIView是OpenGL view,该view用来渲染整个Cocos2D app的显示内容

我们可以通过CCDirector实例的view属性来访问该main UIView。

UISwipeGustureRecognizer为每个滑动方向关联一个方法

iOS的输入处理就是可以这么简单。

猜你在找的Cocos2d-x相关文章