开发者

sort NSArray of UIImagerview?

i want to sort NSArray of UIImageView according their frame?

i try for NSSortDescriptor but when i writ开发者_开发问答e key=@"frame" unrecognized selector error occur. please tell me how to sort my array.

thanks in advance.


You can always write your own comparator function and define custom sorting behavior by using NSArray sortedArrayUsingSelector method


try this code in your project

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

    }];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜