2
我想按照他們的框架排序UIImageView的NSArray?排序UIImagerview的NSArray?
我嘗試使用NSSortDescriptor,但是當我寫key = @「frame」時出現無法識別的選擇器錯誤。 請告訴我如何排序我的數組。
在此先感謝。
我想按照他們的框架排序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;
}
}];