Ios 我可以从视图控制器外部引用子视图吗?

Ios 我可以从视图控制器外部引用子视图吗?,ios,iphone,objective-c,ios7,uigesturerecognizer,Ios,Iphone,Objective C,Ios7,Uigesturerecognizer,我试图在我的“MyScene.m”文件中使用手势识别器来移动屏幕左半部分出现的精灵。为此,我在视图控制器中添加了一个子视图,其框架与屏幕左侧相匹配,并在MyScene中添加了一个手势识别器。手势识别器在MyScene中,因为它调用MyScene中使用同一文件中精灵的方法 问题是我的新子视图“leftView”无法从Myscene引用。有没有办法使新的子视图可以在不同的实现文件中使用 我已将相关代码包含在下面以供参考。我是新来的(把它作为一种爱好),非常感谢你能给我的任何帮助。如果你知道一个整体上

我试图在我的“MyScene.m”文件中使用手势识别器来移动屏幕左半部分出现的精灵。为此,我在视图控制器中添加了一个子视图,其框架与屏幕左侧相匹配,并在MyScene中添加了一个手势识别器。手势识别器在MyScene中,因为它调用MyScene中使用同一文件中精灵的方法

问题是我的新子视图“leftView”无法从Myscene引用。有没有办法使新的子视图可以在不同的实现文件中使用

我已将相关代码包含在下面以供参考。我是新来的(把它作为一种爱好),非常感谢你能给我的任何帮助。如果你知道一个整体上更好的方式来实现我所寻找的结果,我很乐意听到你的建议。谢谢

ViewController.m:

@implementation ViewController

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
      skView.showsFPS = YES;
      skView.showsNodeCount = YES;


      // Create and configure the scene.
      SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
      scene.scaleMode = SKSceneScaleModeAspectFill;
      //skView.scene.physicsWorld.gravity = CGVectorMake(0, skView.scene.physicsWorld.gravity.dy*-1);
      // Present the scene.
      [skView presentScene:scene];

        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width/2, self.view.bounds.size.height)];
        [leftView setBackgroundColor:[UIColor redColor]];
        [leftView setAlpha:0.2];
        [self.view addSubview:leftView];
    }
}
MyScene.m:

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) 
        self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    }
    return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(self) action:@selector(screenSwipedRight)];
    swipeRight.numberOfTouchesRequired = 1;
    swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:(swipeRight)];
    }


-(void)screenSwipedRight
{
    NSLog(@"Screen was swiped to the right");
    CGFloat percentToRight = 1-(self.playerOne.position.x / self.view.bounds.size.width);
    NSTimeInterval timeToRight = self.horizontalRunSpeed * percentToRight;
    NSLog(@"Percent to right = %f",percentToRight);
    NSLog(@"Time to right = %f",timeToRight);
    SKAction *moveNodeRight = [SKAction moveToX:self.view.bounds.size.width-self.playerOne.size.width duration:timeToRight];
    [self.playerOne runAction:[SKAction sequence:@[moveNodeRight]]];
}

rdelmar在评论中回答了这个问题。报价如下:


“与其添加此leftView,为什么不使用手势识别器的locationInView:属性来确定初始触摸在屏幕上的位置,如果它不在左侧,则什么也不做?”-rdelmar


这个解决方案将非常有效。使用locationInView属性将允许仅在屏幕的一侧识别手势。感谢rdelmar和nhgrif花时间提供帮助。

Hmm。。。我发布了一个答案,然后删除了它。事实上,我有点搞不清楚你到底想实现什么。总而言之,我想在用户滑动屏幕的左侧(而且仅在左侧)时触发一个手势识别器。如果他们向右滑动,我不想让人认出这个手势。为此,我在屏幕左侧添加了一个子视图。Objective-C允许您将手势识别器设置为仅在某些视图/子视图中作出反应。问题是,在MyScene.m中,leftView不是一个选项,它会同时影响屏幕上的一个视图?不,它会移动一个精灵。我编辑了之前的评论以添加更多细节。与其添加此leftView,为什么不使用手势识别器的locationInView:属性来确定初始触摸在屏幕上的位置,如果不在左侧,则什么也不做?