排序UIImagerview的NSArray?

问题描述:

我想按照他们的框架排序UIImageView的NSArray?排序UIImagerview的NSArray?

我尝试使用NSSortDescriptor,但是当我写key = @“frame”时出现无法识别的选择器错误。 请告诉我如何排序我的数组。

在此先感谢。

你总是可以编写自己的比较功能,并定义自定义使用NSArray sortedArrayUsingSelector method

排序行为尝试在你的项目这段代码

NSArray * myArray = @[ 

      [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 200, 100)], 
       [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 100)], 
       [[UIImageView alloc] initWithFrame:CGRectMake(50, 40, 50, 100)], 
       [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 200, 100)], 
       [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 67)] 

    ]; 


    myArray = [myArray sortedArrayUsingComparator:^NSComparisonResult(UIImageView * view1, UIImageView * view2) { 

     if (! CGRectIntersectsRect(view1.frame, view2.frame)) { 
      return NSOrderedAscending; 
     } else { 
      return NSOrderedDescending; 
     } 

    }];