Finally:
BeyondViewController.h
beyondviewcontroller.h// 04_ emoticons//// Created by Beyond on 14-7-22.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface Beyondviewcontroller: Uiviewcontroller@property (Weak, nonatomic) Iboutlet Uisegmentedcontrol *segment;-(ibaction) segmentValueChanged: ( Uisegmentedcontrol *) sender; @end
Beyondviewcontroller.m
beyondviewcontroller.m//04_ Emoticons////Created by Beyond on 14-7-22.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h"//Add the button's tag, because the segment value changes, to place the button in the last face of the array # define Kaddbutton_tag 99@interface Beyondviewcontroller () @end @implementation beyondviewcontroller-(void) viewDidLoad{[ Super Viewdidload]; NSLog (@ "View did load"); int colsnum = _segment.selectedsegmentindex + 2; Call your own definition method to join images [self alinmentwithcols:colsnum]; Add a button to the last side, with the aim of clicking the button to add a ImageView to the last [self addbtn];} Add a button to the last side, the purpose is: Click on the button to be able to add a imageview to the last-(void) addbtn{//SG_BTN code generation button 1, instantiation UIButton *btn = [Uibu Tton Alloc]init]; 2, setting details//normal state btn.tag = Kaddbutton_tag; [Btn settitle:@ "normal" forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; [Btn setbackgroundimage:[uiimage imagenamed:@ "Sub_black_add.png"] Forstate:uicontrolstatenormAL]; Highlight state when clicked [Btn settitle:@ "highlighted" forstate:uicontrolstatehighlighted]; [btn Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal]; [Btn setbackgroundimage:[uiimage imagenamed:@ "Sub_blue_add.png"] forstate:uicontrolstatehighlighted]; Add a Click event to the button [BTN addtarget:self action: @selector (Addbtnclick:) forcontrolevents:uicontroleventtouchupinside]; #warning The following is all the permutation coordinates of I calculated by I nsarray *array = self.view.subviews; The automatic arranging of the button is I, the travel number is calculated by I, the column number, coordinates x y int i = array.count-1; Call your own defined method, automatic arranging the index of the control in the grid, figure out which row and column it should be in, and get its coordinates cgpoint btn_pos = [self getpositionbyindexincell:i]; Btn.frame = CGRectMake (btn_pos.x, BTN_POS.Y, 60, 60); 3, add button to Self.view [Self.view addsubview:btn];} By the control located inside the lattice I, that is automatic arranging, to get it in which row which column, thereby returning its x,y-(cgpoint) Getpositionbyindexincell: (int) i{int colsnum = _ Segment.selectedsegmentindex + 2; NSLog (@ "Automatic arranging I in the lattice is%d", i); The No. 0 is the expression of the first pai y int firstimgy = 60; The net spacing between emoticons int imgmargin = (self. View.frame.size.width-60*colsnum)/(colsnum+1); The No. 0 One is the expression x int firstimgx = Imgmargin; The first expression (here is the button) where the line number int row = I/colsnum; The first expression (here is the button) where the column number int cols = I%colsnum; int x = cols * (60+imgmargin) +FIRSTIMGX; int y = row * (60+imgmargin) +firstimgy; return Cgpointmake (x, y);} When you click on the + button, create a random imgview and add it to the Self.view, note the Imgview position, and the location of the btn-(void) Addbtnclick: (UIButton *) sender{//1, class method to create the control uiimageview *imgview = [[Uiimageview alloc]init]; 2, control details int rand = Arc4random_uniform (9); NSString *imgname = [NSString stringwithformat:@ "01%d.png", Rand]; Imgview.image = [UIImage imagenamed:imgname]; Screen width high cgsize winsize = self.view.bounds.size; int orgin_x = Arc4random_uniform (winsize.width); int orgin_y = Arc4random_uniform (winsize.height); Imgview.frame = CGRectMake (orgin_x, orgin_y, 0, 0); 3, add the control to the current controller's view [Self.view Addsubview:imgview]; [UIView animatewithduration:1 animations:^{Animation Implementation 2 Requirements: 1, for the newly added Imgview set X y 2, for the button again set x y nsarray *array = [Self.view subviews]; Automatic arranging is I, I count travel number, column number, coordinate x y int imgview_i = array.count-3; int sender_i = array.count-2; Call your own defined method, automatic arranging the index of the control in the grid, figure out which row and column it should be in, and get its coordinates cgpoint imgview_pos = [self getpositionbyindexincell:imgview_i]; Cgpoint Sender_pos = [self Getpositionbyindexincell:sender_i]; Once again set the location of the newly added imgview and the location of the join button Imgview.frame = CGRectMake (imgview_pos.x, IMGVIEW_POS.Y, 60, 60); Sender.frame = CGRectMake (sender_pos.x, SENDER_POS.Y, 60, 60); }];} Define your own method, Parameters: Image name 010.png ~ 018.png x coordinate y-(void) Addimg: (NSString *) Imgname x: (cgfloat) x y: (cgfloat) y{//1, instantiating UI ImageView *imgview = [[Uiimageview alloc]init]; 2, setting details UIImage *img = [UIImage imagenamed:imgname]; Imgview.image = img; Imgview.frame = CGRectMake (x, Y, 60, 60); 3, add to the self view inside to show [Self.view Addsubview:imgview]; }-(ibaction) segmenTvaluechanged: (Uisegmentedcontrol *) Sender {//index value is 0 1 2 3-------2 column 3 column 4 column 5 column//NSLog (@ "%d", sender.selectedsegmen Tindex); int segmentindex = Sender.selectedsegmentindex; int colsnum = Segmentindex + 2; Before adjusting the position, first in the Self.view.subViews array, add the button to the most position//through the tag to AddButton UIButton *addbtn = [Self.view viewwithtag: Kaddbutton_tag]; In order to be able to always let addbtn in the last Self.view, first move it out of the parent control, and then add to the parent control [ADDBTN Removefromsuperview]; [Self.view ADDSUBVIEW:ADDBTN]; Call your own definition method to adjust the original position [self alinmentwithcols:colsnum]; }//extracted from the method, divided into adding and removing again 2 processing methods, the inference method is that the number of child controls in the view is only one segmentcontrol-(void) Alinmentwithcols: (int) colsnum{// Assuming that the number of child controls is 1 only segmentcontrol this one control, then join, otherwise, again adjust position int subviewscount = [[Self.view subviews] count]; NSLog (@ "Number of child controls:%d---", subviewscount); Extract the common code int n = subViewsCount-1; Suppose n = 0, indicating that there is only one segment, at which time to add 3 imageview//Otherwise, remove all the child controls, and set the position for (int i=0; i<= (n==0?3:n-1); i++) { AdjustableUsing your own defined method, the index of the control in the grid is automatic arranging, which row, which column should be calculated, and thus get its coordinates cgpoint pos = [self getpositionbyindexincell:i]; Different processing methods if (Subviewscount = = 1) {NSString *imgname = [NSString stringwithformat:@ "01%d.png", I]; [Self addimg:imgname x:pos.x y:pos.y]; } else {//when clicking Segment, you should not add ImageView again, but should remove, and set xy//[self addimg:imgname x:x y:y] again; Nsarray *array = [Self.view subviews]; NSLog (@ "setting%@ position Again", [Array[i+1] class]); UIView *view = array[i+1]; [UIView animatewithduration:1 animations:^{///The following three steps are OC Standard Code, because OC does not agree to directly fix the value of the member of the struct attribute in the object, to pass the middle of the temporary structure of the body variable CGRect frame = view.frame; Frame.origin = Cgpointmake (pos.x, POS.Y); View.frame=frame; }]; }//End of If else}//End of For loop}//End of Function@end
1
2
ios_4_ expression Arrangement