04-图片排列切换
ViewController.h文件中:
1 @interface ViewController : UIViewController 2 3 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentCtrl; 4 5 - (IBAction)indexChange:(UISegmentedControl *)sender; 6 7 @end
ViewController.m文件中:
1 #import "ViewController.h" 2 3 #define kImgWH 40 4 #define kExistCtrl 2 // 新增ImageView控件之前已存在的控件数目 5 6 @interface ViewController () 7 { 8 int _imgCount; // 图片控件的个数 9 UIButton *_btn; 10 } 11 12 @end 13 14 @implementation ViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 //初始化图片控件个数 20 _imgCount = 9; 21 22 // 先新增按钮控件(新增顺序不可调换) 23 _btn = [[UIButton alloc] init]; 24 [_btn setTitle:@"新增" forState:UIControlStateNormal]; 25 [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 26 [self.view addSubview:_btn]; 27 // 监听事件 28 [_btn addTarget:self action:@selector(addImg) forControlEvents:UIControlEventTouchUpInside]; 29 30 // 再新增图片控件 31 [self adjustImagePosWithColumns:2]; 32 } 33 34 #pragma mark 调整图片位置的封装函数 35 - (void)adjustImagePosWithColumns:(int)columns 36 { 37 CGFloat marginW = (self.view.frame.size.width - columns * kImgWH)/(columns + 1); 38 CGFloat marginH = 20; 39 CGFloat oneY = 80; 40 41 // 调整图片或新增图片控件 42 for (int i = 0; i < _imgCount; ++i) { 43 int col = i % columns; 44 int row = i / columns; 45 46 int x = marginW + col * (marginW + kImgWH); 47 int y = oneY + row * (marginH + kImgWH); 48 49 CGRect frm = CGRectMake(x, y, kImgWH, kImgWH); 50 51 if (self.view.subviews.count < _imgCount + kExistCtrl) { 52 //动态创建UIImageView控件 53 int no = i % 9; 54 NSString *name = [NSString stringWithFormat:@"01%d.png", no]; 55 56 UIImageView *img = [[UIImageView alloc] init]; 57 img.image = [UIImage imageNamed:name]; 58 img.frame = frm; 59 60 [self.view addSubview:img]; 61 } 62 else { 63 // 调整图片的位置 64 UIView *child = self.view.subviews[i + kExistCtrl]; 65 child.frame = frm; 66 } 67 } 68 69 // 调整按钮的位置 70 int col = _imgCount % columns; 71 int row = _imgCount / columns; 72 int x = marginW + col * (marginW + kImgWH); 73 int y = oneY + row * (marginH + kImgWH); 74 75 CGRect frm = CGRectMake(x, y, kImgWH, kImgWH); 76 _btn.frame = frm; 77 78 // NSLog(@"子控件个数=%lu", (unsigned long)self.view.subviews.count); 79 } 80 81 #pragma mark Segmented控件的值变化的监听事件 82 - (IBAction)indexChange:(UISegmentedControl *)sender { 83 84 [UIView beginAnimations:nil context:nil]; 85 [UIView setAnimationDuration:0.5]; 86 87 int columns = (int)(sender.selectedSegmentIndex + 2); 88 [self adjustImagePosWithColumns:columns]; 89 90 [UIView commitAnimations]; 91 } 92 93 #pragma mark “新增”按钮的响应事件 94 - (void)addImg 95 { 96 // 新增图片控件 97 int no = _imgCount % 9; 98 NSString *name = [NSString stringWithFormat:@"01%d.png", no]; 99 100 UIImageView *img = [[UIImageView alloc] init]; 101 img.image = [UIImage imageNamed:name]; 102 img.frame = CGRectMake(_btn.frame.origin.x, _btn.frame.origin.y, 0, 0); 103 // img.frame = CGRectMake(0, 0, kImgWH, kImgWH); 104 [self.view addSubview:img]; 105 106 [UIView beginAnimations:nil context:nil]; 107 [UIView setAnimationDuration:0.5]; 108 109 _imgCount += 1; 110 int columns = (int)(self.segmentCtrl.selectedSegmentIndex + 2); 111 [self adjustImagePosWithColumns:columns]; 112 113 [UIView commitAnimations]; 114 } 115 116 117 @end
界面效果: