【问题标题】:Simple layer problem [iphone]简单层问题[iphone]
【发布时间】:2010-01-18 11:42:33
【问题描述】:

基本上,我是按 y 值对数组中的对象进行排序(页面最下方位于数组的开头),但我遇到了一些麻烦。我为数组中的所有 UIImageViews 分配了一个值:

for (UIImageView *Blocky in objectsArray){
  [Blocky.layer setValue:[NSString stringWithFormat: @"%f",
                                     Blocky.center.y] forKey:@"value"];
}

因为它是 UIImageView 我必须在“Blocky”之后放置图层,否则我会收到以下错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIImageView 0x3d44880> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key value.'

我的问题是当我对它进行排序时,我不知道将“.layer”放在哪里,所以我遇到了同样的问题,因为 UIImageViews 不能自己处理键。这是我的排序代码:

NSSortDescriptor *sortDescriptor =
  [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[objectsArray sortUsingDescriptors:[NSArray
                                    arrayWithObject:sortDescriptor]];
[sortDescriptor release];

提前致谢, 奥兹

【问题讨论】:

    标签: iphone layer


    【解决方案1】:

    使用 NSMutableArray sortUsingFunction:context: 方法:

    // top of file
    static NSInteger compareImageHeights(id image1, id image2, void* context) {
      CGFloat temp = ((UIImageView*)image2).center.y - ((UIImageView*)image1).center.y;
      return (temp>0)?NSOrderedAscending:(temp<0)?NSOrderedDescending:NSOrderedSame;
    }
    // within file somewhere
    [objectsArray sortUsingFunction:compareImageHeights context:NULL];
    

    需要该函数的原因(并且您不能只使用 sortUsingSelector: 或 sortUsingSortDescriptor:)是您不能指定 KVC 键来仅将 y 组件标识为排序only变量。

    编辑:将center.y 更改为size.height

    编辑:糟糕。将size.height 更改回center.y。将类型固定为 UIImageView 而不是 UIImage。似乎 KVC 对象理论上可以支持任意键,但有些支持,有些不支持。

    【讨论】:

    【解决方案2】:

    您确定 CALayer 应该接受任意 KVC 密钥吗?这在我看来是错误的。

    您不应该将高度提取到结构缓冲区和qsort 吗?

    例如

    我想应该是这样的:

    //
    // before @implementation sections
    //
    typedef struct __MySortEntry {
      UIImageView* image;
      CGFloat height;
    } MySortEntry, *PMySortEntry;
    
    static int compareMySortEntry (const void * a, const void * b)
    {
      CGFloat temp =( ((PMySortEntry)a)->height - ((PMySortEntry)b)->height );
      return (temp<0)?-1:(temp>0)?1:0;
    }
    
    //
    // in @implementation section somewhere
    //
    NSMutableData* mutable = [[NSMutableData alloc] initWithLength:
      sizeof(MySortEntry)*objectsArray.count];
    PMySortEntry entries = (PMySortEntry)mutable.mutableBytes;
    for (int c = 0; c < objectsArray.count; c++) {
      entries[c].image = (UIImageView*)[objectsArray objectAtIndex:c];
      entries[c].height = entries[c].image.center.y;
    }
    
    qsort(entries, objectArray.count, sizeof(MySortEntry), compareMySortEntry);
    
    for (int c=0; c < objectArray.count; c++) {
      UIImage* imageInSequence = entries[c].image;
      // do something with images **in sequence**
    }
    [mutable release];
    

    编辑:将center.y 更改为size.height

    编辑:将size.height 改回center.y。哎呀。

    编辑:将 UIImage 更改为 UIImageView。

    【讨论】:

    • 对不起!我所说的高度是指哪个具有最大的 y 值而不是最大的图像
    • CALayers 是符合键值编码的容器类,因此它们可以接受任意键和值。
    【解决方案3】:

    为什么不在每个视图上设置标签属性,因为您根据高度创建它们并通过标签检索它们?

    【讨论】:

      猜你喜欢
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      相关资源
      最近更新 更多