04-switching image arrangement, 04-image Arrangement
ViewController. h file:
1 @interface ViewController : UIViewController2 3 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentCtrl;4 5 - (IBAction)indexChange:(UISegmentedControl *)sender;6 7 @end
In the ViewController. m file:
1 # import "ViewController. h "2 3 # define kImgWH 40 4 # define kExistCtrl 2 // Add the number of existing controls before the ImageView control 5 6 @ interface ViewController () 7 {8 int _ imgCount; // Number of image controls 9 UIButton * _ btn; 10} 11 12 @ end 13 14 @ implementation ViewController 15 16-(void) viewDidLoad {17 [super viewDidLoad]; 18 19 // initialize the number of image controls 20 _ imgCount = 9; 21 22 // Add a button control first (the order of addition cannot be changed) 23 _ btn = [[UIButton alloc] init]; 24 [_ btn setTitle: @ "new" forState: UIControlStateNormal]; 25 [_ btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; 26 [self. view addSubview: _ btn]; 27 // listener event 28 [_ btn addTarget: self action: @ selector (addImg) forControlEvents: UIControlEventTouchUpInside]; 29 30 // Add image control 31 [self adjustImagePosWithColumns: 2]; 32} 33 34 # pragma mark function 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 // adjust the image or add an image control 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 // dynamically create the UIImageView control 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 // adjust the image position 64 UIView * child = self. view. subviews [I + kExistCtrl]; 65 child. frame = frm; 66} 67} 68 69 // adjust the button position 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 (@ "Number of child controls = % lu", (unsigned long) self. view. subviews. count); 79} 80 81 # Listening event of pragma mark Segmented control value change 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 # RESPONSE event 94-(void) of The pragma mark "add" button) addImg 95 {96 // added the image control 97 int no = _ imgCount % 9; 98 NSString * name = [NSString stringWithFormat: @ "01366d.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 114 [UIView commitAnimations]; 115} 116 117 @ end
Interface effect: