CCScrollView滚动和触摸事件从不触发 [英] CCScrollView scroll and touch events never firing

查看:199
本文介绍了CCScrollView滚动和触摸事件从不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何有用的教程或如何使用 CCScrollView 的说明。我有一个网格布局的精灵和标签(列出一个iOS游戏的成就)。



要滚动,用户可以向上滑动/向上滚动,以显示精灵等等。



我找到了一些代码示例,他们似乎表明你只需要将你的内容节点添加到滚动节点,它会小心的其余部分。



似乎不工作。没有滚动,滚动层上的平移/触摸事件似乎没有触发。



我没有使用SpriteBuilder。

  //节点保存所有精灵/标签
scrollContents = [CCNode node];

//在网格视图中添加一些sprites /标签
(badgeKeys中的NSString * key){

//加载sprite的逻辑将在这里

CCSprite * badge = [CCSprite spriteWithSpriteFrame:frame];
badge.positionType = CCPositionTypeNormalized;
badge.position = ccp(xPos,yPos);
[scrollContents addChild:badge];

//一些逻辑用于增量x / y位置逻辑,用于网格布局

}

//滚动视图
scrollView = [[CCScrollView alloc] initWithContentNode:scrollContents];
scrollView.horizo​​ntalScrollEnabled = NO;
scrollView.verticalScrollEnabled = YES;
[scrollView setBounces:NO];

//我的sprites从来没有显示除非我手动设置
scrollContents.contentSize = CGSizeMake(self.contentSize.width,960);
NSLog(@scrollContents contentize:%f%f,scrollContents.contentSize.width,scrollContents.contentSize.height);

[self addChild:scrollView];


解决方案

确定,这里是一个工作示例的我的代码,给你一个完全工作的代码示例)滚动菜单与'活'按钮里面。我只是测试这个解构,它工作

   - (void)scrollingMenuWithCharmsTest {

设置东西滚动

GameInventory * gi = [GameInventory sharedGameInventory];
while(gi.armorCharms.count< 20){
[gi addArmorCharm:[ArmorCharm createRandomArmorCharm]];
}

CCNode * contentNode = [self charmsContentNodeFor:gi.armorCharms
showEquiped:NO
spacingBetweenMenuItems:8
target:self
selector :@selector(onArmorCharmSelected :)];

//设置裁剪节点裁剪CCScrollingMenu


CCNodeColor * ccn = [CCNodeColor nodeWithColor:[CCColor blackColor] width:180 height:200] ;
ccn.anchorPoint = ccp(0,0);
CCClippingNode * cn = [CCClippingNode clippingNodeWithStencil:ccn];
cn.alphaThreshold = 0.05f;
[self addChild:cn];
cn.inverted = NO;
cn.positionInPointsV = ccp(50,50);
cn.anchorPoint = ccp(0,0);
cn.contentSizeInPoints = CGSizeMake(180,200);

//设置滚动菜单

CCScrollView * bsm = [[CCScrollView alloc] initWithContentNode:contentNode];
bsm.contentSize = CGSizeMake(180,200);

[cn addChild:bsm];
bsm.position = ccp(0,0);
bsm.bounces = YES;
bsm.pagingEnabled = NO;
bsm.verticalScrollEnabled = YES;
bsm.horizo​​ntalScrollEnabled = NO;

bsm.contentSizeInPoints = CGSizeMake(180,200); // inPoints只在节点有父节点之后

for(CharmAbstractBoxMenuItem * lmi in bsm.contentNode.children){
TRACE(@item%@ is at%@,lmi。 item.description,NSStringFromCGPoint(lmi.positionInPointsV));
}
TRACE(@页数:%i,bsm.numVerticalPages);

}

- (CCNode *)charmsContentNodeFor:(NSDictionary *)keyedItems
showEquiped:(BOOL)isShowEquiped
spacingBetweenMenuItems:(float)inSpacing
target:(id)inTarget
selector:(SEL)inSelector {

NSSortDescriptor * sortOrder = [NSSortDescriptor sortDescriptorWithKey:@selfascending:YES];
NSArray * sortedKeys = [[keyedItems allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortOrder]];

float initialY = 0;
float currentY = initialY;

NSUInteger itemNumber = 0;
CGFloat width = 0;
CGFloat height = 0;
CCNode * contentNode = [CCNode node];
for(NSUInteger loopi = 0; loopi< [sortedKeys count]; loopi ++){
NSString * key = [sortedKeys objectAtIndex:loopi];
CharmAbstract * ci = [keyedItems objectForKey:key];

if(ci){
CharmAbstractBoxMenuItem * cmi = [CharmAbstractBoxMenuItem itemBoxFor:ci
target:inTarget
selector:inSelector
];

cmi.toolTip = ci.toolTip;
cmi.position = ccp(deviceOffset(0),currentY);
cmi.key = key;
[contentNode addChild:cmi z:0 name:[NSString stringWithFormat:@%li,(long)itemNumber]];

currentY + = cmi.contentSizeInPoints.height + inSpacing;
itemNumber ++;
if(cmi.contentSize.width> width)width = cmi.contentSize.width;
height + = cmi.contentSize.height;
if(loopi< sortedKeys.count - 1)height + = inSpacing;
}
else {
MPLOG(@*** Key [%@] yielded no items。,key);
}

}
contentNode.contentSizeType = CCSizeTypePoints;
contentNode.contentSize = CGSizeMake(width,height);
return contentNode;

}

一些备注:




  • 我给你我的构建内容节点例程,以便您知道位置和大小的内容。

  • 我的charmBoxMenuItemss派生自'CCButton'和热...在这个代码段的完整版本中,我扩展CCScrollView,以防止按钮在裁剪区域外虽然它们是从视图中裁剪出来的,但它们在默认情况下仍然是可见的,并且当随机抽头发生在裁剪区域上方或下方时可以响应。)

  • 您需要在您的setupCocos2dWithOptions行中添加:



    CCSetupDepthFormat:[NSNumber numberWithUnsignedInt:GL_DEPTH24_STENCIL8_OES]





    • I can't find any helpful tutorials or explanation on how to use a CCScrollView. I have a grid-layout of sprites and labels (listing achievements for an iOS game). There are more than can fit on the screen so I want the user to be able to scroll.

      To scroll, the user would swipe/pan upwards, to reveal the sprites etc which are lower.

      I've found a few code samples and they seem to indicate you just need to add your content node to the scroll node and it will take care of the rest.

      It doesn't seem to work. There's no scroll, and the pan/touch events on the scroll layer never seem to fire. The close button I have at the same child (sibling to the scroll view) no longer works as well.

      I'm not using SpriteBuilder.

      // Node to hold all sprites/labels
      scrollContents = [CCNode node];
      
      // I add a bunch of sprites/labels in a grid view
      for( NSString *key in badgeKeys ){
      
          // logic to load the sprite would be here
      
          CCSprite *badge = [CCSprite spriteWithSpriteFrame:frame];
          badge.positionType = CCPositionTypeNormalized;
          badge.position = ccp(xPos,yPos);
          [scrollContents addChild:badge];
      
          // some logic to increment x/y position logic, for grid layout
      
      }
      
      // Scroll view
      scrollView = [[CCScrollView alloc] initWithContentNode:scrollContents];
      scrollView.horizontalScrollEnabled = NO;
      scrollView.verticalScrollEnabled = YES;
      [scrollView setBounces:NO];
      
      // My sprites never even show unless I manually set this
      scrollContents.contentSize = CGSizeMake(self.contentSize.width,960);
      NSLog(@"scrollContents contentsize: %f %f", scrollContents.contentSize.width,scrollContents.contentSize.height);
      
      [self addChild:scrollView];
      

      解决方案

      ok, here is a working example (i deconstructed part of my code to give you a fully working code sample) of a scrolling menu with 'live' buttons inside. I just tested this 'deconstruction' , it works

      - (void) scrollingMenuWithCharmsTest {
      
      //    setup something to scroll
      
      GameInventory *gi = [GameInventory sharedGameInventory];
      while (gi.armorCharms.count < 20) {
          [gi addArmorCharm:[ArmorCharm createRandomArmorCharm]];
      }
      
      CCNode *contentNode = [self charmsContentNodeFor:gi.armorCharms
                                           showEquiped:NO
                               spacingBetweenMenuItems:8
                                                target:self
                                              selector:@selector(onArmorCharmSelected:)];
      
      // setup a clipping node to crop out the CCScrollingMenu
      
      
      CCNodeColor *ccn = [CCNodeColor nodeWithColor:[CCColor blackColor] width:180 height:200];
      ccn.anchorPoint = ccp(0, 0);
      CCClippingNode *cn = [CCClippingNode clippingNodeWithStencil:ccn];
      cn.alphaThreshold = 0.05f;
      [self addChild:cn];
      cn.inverted            = NO;
      cn.positionInPointsV   = ccp(50, 50);
      cn.anchorPoint         = ccp(0, 0);
      cn.contentSizeInPoints = CGSizeMake(180, 200);
      
      // setup scrolling menu
      
      CCScrollView * bsm = [[CCScrollView alloc] initWithContentNode:contentNode];
      bsm.contentSize=CGSizeMake(180,200);
      
      [cn addChild:bsm];
      bsm.position                = ccp(0, 0);
      bsm.bounces                 = YES;
      bsm.pagingEnabled           = NO;
      bsm.verticalScrollEnabled   = YES;
      bsm.horizontalScrollEnabled = NO;
      
      bsm.contentSizeInPoints = CGSizeMake(180, 200); // inPoints only after the node has a parent
      
      for (CharmAbstractBoxMenuItem *lmi in bsm.contentNode.children) {
          TRACE(@"item %@ is at %@", lmi.item.description, NSStringFromCGPoint(lmi.positionInPointsV));
      }
      TRACE(@"number of pages : %i", bsm.numVerticalPages);
      
      }
      
      - (CCNode *)charmsContentNodeFor:(NSDictionary *)keyedItems
                           showEquiped:(BOOL)isShowEquiped
               spacingBetweenMenuItems:(float)inSpacing
                                target:(id)inTarget
                              selector:(SEL)inSelector {
      
      NSSortDescriptor *sortOrder  = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
      NSArray          *sortedKeys = [[keyedItems allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortOrder]];
      
      float initialY = 0;
      float currentY = initialY;
      
      NSUInteger      itemNumber   = 0;
      CGFloat         width        = 0;
      CGFloat         height       = 0;
      CCNode          *contentNode = [CCNode node];
      for (NSUInteger loopi        = 0; loopi < [sortedKeys count]; loopi++) {
          NSString      *key = [sortedKeys objectAtIndex:loopi];
          CharmAbstract *ci  = [keyedItems objectForKey:key];
      
          if (ci) {
              CharmAbstractBoxMenuItem *cmi = [CharmAbstractBoxMenuItem itemBoxFor:ci
                                                                            target:inTarget
                                                                          selector:inSelector
              ];
      
              cmi.toolTip  = ci.toolTip;
              cmi.position = ccp(deviceOffset(0), currentY);
              cmi.key      = key;
              [contentNode addChild:cmi z:0 name:[NSString stringWithFormat:@"%li", (long) itemNumber]];
      
              currentY += cmi.contentSizeInPoints.height + inSpacing;
              itemNumber++;
              if (cmi.contentSize.width > width) width = cmi.contentSize.width;
              height += cmi.contentSize.height;
              if (loopi < sortedKeys.count - 1) height += inSpacing;
          }
          else {
              MPLOG(@"*** Key [%@] yielded no items.", key);
          }
      
      }
      contentNode.contentSizeType = CCSizeTypePoints;
      contentNode.contentSize     = CGSizeMake(width, height);
      return contentNode;
      
      }
      

      some notes :

      • i gave you my 'build content node' routine so you know the ins and outs of positions and sizes.
      • my charmBoxMenuItemss derive from 'CCButton' and are hot ... In the full version of this code snippet, i extended CCScrollView to prevent the buttons from being 'hot' outside the crop area (although they are cropped out from view, they are still 'visible' by default, and could respond when a random tap occurs above or below the crop area).
      • For clipping node with stencil, you need to add this in your setupCocos2dWithOptions line:

        CCSetupDepthFormat : [NSNumber numberWithUnsignedInt:GL_DEPTH24_STENCIL8_OES]

      这篇关于CCScrollView滚动和触摸事件从不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆