ios_4_ expression Arrangement

Source: Internet
Author: User

Eventually:



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 Tag of the button, because when the segment value changes, place the button on 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 the custom method to add images [self alinmentwithcols:colsnum]; Add a button to the last side to add a imageview to the last [self addbtn] by clicking on the button:} Add a button to the last face, the purpose is: Click the button to add a ImageView to the last-(void) addbtn{//SG_BTN code generation button 1, instantiation UIButton *btn = [[UIButton alloc]i    NIT];    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 status 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 for the button [BTN addtarget:self action: @selector (Addbtnclick:) forcontrolevents:uicontroleventtouchupinside];    #warning below all the permutation coordinates of I calculated by I nsarray *array = self.view.subviews;    The automatic arranging of the button is I, by I count travel number, column number, coordinate x y int i = array.count-1;    Call a custom method, automatic arranging the index of the control in the grid, calculate which row it should be in, which column to 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; 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) is 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 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 creation    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, reset the button 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 a custom method, automatic arranging the index of the control in the grid, calculate which row it should be in, which column to get its coordinates cgpoint imgview_pos = [self getpositionbyindexincell:imgview_i];                        Cgpoint Sender_pos = [self Getpositionbyindexincell:sender_i];        Reset the location of the newly added imgview and the location of the Add button Imgview.frame = CGRectMake (imgview_pos.x, IMGVIEW_POS.Y, 60, 60);    Sender.frame = CGRectMake (sender_pos.x, SENDER_POS.Y, 60, 60); }];} Custom methods, Parameters: Picture name 010.png ~ 018.png x coordinate y coordinate-(void) Addimg: (NSString *) Imgname x: (cgfloat) x y: (cgfloat) y{//1, instantiating UII    Mageview *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 view of self to display [Self.view Addsubview:imgview]; }-(ibaction) segmentvaluechanged: (Uisegmentedcontrol *) Sender {//index value is 0 1 2 3-------2 Columns 3 columns 4 columns 5 columns//NSLog (@ "%d", sender.selectedsegmentindex);    int segmentindex = Sender.selectedsegmentindex;        int colsnum = Segmentindex + 2; Before adjusting the position, first in the self.view.subViews array, put the Add button to the most position//through the tag to AddButton UIButton *addbtn = [Self.view viewwithtag:kadd    Button_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 the custom method to adjust the original position [self alinmentwithcols:colsnum]; }//extraction method, divided into Add and remove re-adjust 2 ways to determine whether the number of child controls in the view is only one segmentcontrol-(void) Alinmentwithcols: (int) colsnum{//    If the number of child controls is 1 that is only segmentcontrol this one control, then add, otherwise, reposition int subviewscount = [[Self.view subviews] count];    NSLog (@ "Number of child controls:%d---", subviewscount);    Extract the common code int n = subViewsCount-1;                If n = 0, the description is only one segment, at this point to add 3 imageview//Otherwise, remove all child controls, reset the location for (int i=0; i<= (n==0?3:n-1); i++) { Call a custom method by automatic arranging the index of the control in the grid, figure out which row it should be in, which column to 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, reset XY//[self addimg:imgname x:x y:y];            Nsarray *array = [Self.view subviews];            NSLog (@ "re-setting%@ position", [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 allow directly to repair the value of the member of the struct property in the object, to pass the intermediate temporary structure 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.