1

基本的に、配列内のオブジェクトをy値で並べ替えています(ページの一番下が配列の先頭にあります)が、少し問題があります。配列内のすべてのUIImageViewに値を割り当てます。

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];

よろしくお願いします、オジー

4

3 に答える 3

0

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 に変更しました。

于 2010-01-18T12:07:19.000 に答える
0

高さに基づいてビューを作成し、タグで取得するときに、各ビューにタグ プロパティを設定してみませんか?

于 2010-01-20T17:20:12.723 に答える
0

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 コンポーネントのみをソート変数としてのみ識別することができないためです。

編集: に変更center.yされましたsize.height

編集:おっと。に戻しsize.heightましたcenter.y。タイプを UIImage ではなく UIImageView に固定しました。KVC オブジェクトは理論的には任意のキーをサポートできるようですが、そうするものとそうでないものがあります。

于 2010-01-18T12:30:26.793 に答える