2010-07-31 5 views
2

프레임에 따라 UIImageView의 NSArray를 정렬하고 싶습니까?UIImagerview의 NSArray를 정렬 하시겠습니까?

NSSortDescriptor를 시도하지만 키 = @ "프레임"을 쓸 때 인식 할 수없는 선택기 오류가 발생합니다. 배열을 정렬하는 법을 알려주십시오.

미리 감사드립니다.

답변

0

를 사용하여 동작을 사용자 지정 정렬 정의 할 수 있습니다

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

    }];