圖片排列切換
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
界面效果: