Skip to content

Commit

Permalink
dd
Browse files Browse the repository at this point in the history
  • Loading branch information
贾翊玮_jyw authored and 贾翊玮_jyw committed Jan 6, 2017
1 parent e3a2c75 commit 547cd9e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
Binary file not shown.
73 changes: 45 additions & 28 deletions 冒泡可视化demo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#import <UIKit/UIKit.h>
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

@interface ViewController ()
@property (nonatomic,strong)NSMutableArray *sortViews;
@property (nonatomic,strong)NSMutableArray *resurlts;
@end

@implementation ViewController
Expand All @@ -28,45 +30,60 @@ - (void)viewDidLoad {
NSInteger arcNum = arc4random()%sortNumMax;
arcNum = arcNum == 0 ? 1 : arcNum;
SortView *view = [[SortView alloc] initWithFrame:frame WithNum:arcNum];
view.tag = i;
view.backgroundColor = [UIColor orangeColor];
[self.sortViews addObject:view];
[self.view addSubview:view];
}


self.resurlts = [[NSMutableArray alloc] init];
}

- (IBAction)startAction:(id)sender {

[self.sortViews sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
SortView* view1 = (SortView *)obj1 ;
SortView *view2 = (SortView *)obj2 ;
CGRect frame1 = view1.frame;
CGRect frame2 = view2.frame;
CGRect tempFrme = frame1;
if (view1.frame.size.height > view2.frame.size.height) {
tempFrme = frame1;
frame1.origin.x = frame2.origin.x;
frame2.origin.x = tempFrme.origin.x;
view2.frame = frame2;
view1.frame = frame1;
return NSOrderedDescending;
}
if (view1.frame.size.height < view2.frame.size.height) {
tempFrme = frame1;
frame1.origin.x = frame2.origin.x;
frame2.origin.x = tempFrme.origin.x;
view2.frame = frame2;
view1.frame = frame1;
return NSOrderedAscending;
}
return NSOrderedSame;
}];

[self.resurlts removeAllObjects];
NSInteger o = 0;
for (NSInteger i = 0; i < self.sortViews.count; i ++) {
NSInteger j = self.sortViews.count - 1;
while (j > i) {

for (NSInteger j = i+ 1; j < self.sortViews.count; j ++) {


SortView* view1 = (SortView *)self.sortViews[i] ;
SortView *view2 = (SortView *)self.sortViews[j];
CGRect frame1 = view1.frame;
CGRect frame2 = view2.frame;
if (frame1.size.height > frame2.size.height) {
[self.sortViews exchangeObjectAtIndex:i withObjectAtIndex:j];
[self.resurlts addObject:view2];
o ++;
}
}
}
for (SortView *view in self.resurlts) {
NSLog(@"%zd",view.tag);

}
NSLog(@"o==%zd",o);


}
- (void)exChangeSortView:(SortView *)view1 With:(SortView *)view2
{


__block CGRect frame1 = view1.frame;
__block CGRect tempFrme = frame1;
__block CGRect frame2 = view2.frame;
tempFrme = frame1;
[UIView animateWithDuration:.8 animations:^{
frame1.origin.x = frame2.origin.x;
frame2.origin.x = tempFrme.origin.x;
view2.frame = frame2;
view1.frame = frame1;
} completion:^(BOOL finished) {
[NSThread sleepForTimeInterval:1];
}];

}

- (void)didReceiveMemoryWarning {
Expand Down

0 comments on commit 547cd9e

Please sign in to comment.