为触摸和拖动节点创建更大的字段,目标c

时间:2016-04-15 08:30:10

标签: ios objective-c cocoa-touch sprite-kit skspritenode

我正在制作可以在定义区域内触摸和拖动的小桨节点。问题是桨很小,这使得手指没有反应。我的问题是如何在不使节点更大的情况下使节点周围的区域更大。以下是我的工作内容:

- (id)initWithName:(NSString *)name
{
    self = [super init];
      if (self) {

    self = [PaddleNode spriteNodeWithImageNamed: @"board.png"];
    self.userInteractionEnabled = YES;
    self.name = name;
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
    self.physicsBody.dynamic = NO;
    self.physicsBody.usesPreciseCollisionDetection = YES;
    self.physicsBody.categoryBitMask = paddleCategory;
    self.physicsBody.collisionBitMask = emptyCategory;
    self.currentTouchValue = nil;
}

   return self;
}

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
        self.previousTouchesTimestamp = event.timestamp;


for (UITouch *touch in touches) {
    self.currentTouchValue = [NSValue valueWithNonretainedObject:touch];
}
 }

   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 {

self.currentTouchValue = nil;
  }

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   {
for (UITouch *touch in touches) {
    NSValue *key = [NSValue valueWithNonretainedObject:touch];
    if ([key isEqualToValue:self.currentTouchValue]) {

        CGPoint touchPoint = [touch locationInNode:self.scene];
        CGPoint movePoint = CGPointMake(self.position.x, touchPoint.y);
        if ([self withinParentFrame:movePoint])
            self.position = movePoint;
    }
}

  }

 - (BOOL)withinParentFrame:(CGPoint)point
 {
CGFloat offset = self.size.height / 2;
if (point.y >= offset && point.y <= self.scene.frame.size.height - offset)
    return YES;
else
    return NO;
 }

已编辑:我已阅读

  

UITouch类参考

文档。据我所知,我不能在 touchesMoved 中做到,但我发现我的旧项目,是相同的代码,但我可以把手指放在屏幕上的任何地方,节点将在节点中移动定义区域。
已编辑2 这个问题仍然没有答案,对我很重要!

0 个答案:

没有答案