iOS learning-the arrangement of emoticons

Source: Internet
Author: User

viewcontroller.m//Emoticons////Created by on 15/4/15.//Copyright (c) 2015 Apple. All rights reserved.//#import "ViewController.h" #define Kimagewidth 40@interface Viewcontroller () {UIButton *_addimage    ;    Nsinteger _imagecount;    Nsinteger _columns;    Nsinteger _col;    Nsinteger _row;    CGFloat _onex;    CGFloat _oney; CGFloat _margin;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    _imagecount = 9; [Self changepointofimage:2];} -(void) AddImage: (NSString *) imageName Pointx: (nsinteger) x Pointy: (Nsinteger) y {uiimageview *faceview = [[Uiimageview]    Alloc] Initwithframe:cgrectmake (x, Y, Kimagewidth, kimagewidth)];    Faceview.image = [UIImage imagenamed:imagename]; [Self.view Addsubview:faceview];}        -(void) Changepointofimage: (Nsinteger) currentcolumns {_columns = Currentcolumns;    _margin = (Self.view.frame.size.width-currentcolumns * kimagewidth)/(Currentcolumns + 1);    _onex = _margin;        _oney = 120; for (NSInteger i = 0; I <= _imagecount;  i++) {_col = i% Currentcolumns;  Number of columns _row = I/currentcolumns;        Number of rows CGFloat x = _onex + _col * (kimagewidth + _margin);                CGFloat y = _oney + _row * (kimagewidth + _margin);            if (Self.view.subviews.count = = 4 + _imagecount) {UIView *temp = self.view.subviews[i + 3];            CGRect frame = temp.frame;            Frame.origin = Cgpointmake (x, y);        Temp.frame = frame;            } else if (i = = _imagecount) {_addimage = [UIButton buttonwithtype:uibuttontypecontactadd];            [_addimage addtarget:self Action: @selector (Addfaceimage) forcontrolevents:uicontroleventtouchupinside];            _addimage.frame = CGRectMake (x, Y, Kimagewidth, kimagewidth);        [Self.view Addsubview:_addimage];            } else {Nsinteger imageindex = i% 9;            NSString *imagename = [NSString stringwithformat:@ "01%ld.png", ImageIndex]; [Self Addimage:imAgename pointx:x Pointy:y];    }}}-(void) addfaceimage {[UIView beginanimations:nil context:nil];        [UIView setanimationduration:0.35];    _imagecount++;    Nsinteger newindex = _imagecount% 9;        NSString *imagename = [NSString stringwithformat:@ "01%ld.png", NewIndex];    CGFloat x = _addimage.frame.origin.x;        CGFloat y = _addimage.frame.origin.y;        [Self addimage:imagename pointx:x pointy:y];     _col = _imagecount% _columns;        _row = _imagecount/_columns;    CGRect frame = _addimage.frame;    frame.origin.x = _onex + _col * (kimagewidth + _margin);    FRAME.ORIGIN.Y = _oney + _row * (kimagewidth + _margin);        _addimage.frame = frame; [UIView commitanimations];} -(void) didreceivememorywarning {[Super didreceivememorywarning];}    -(Ibaction) Segmentedchoose: (Uisegmentedcontrol *) Sender {[UIView beginanimations:nil context:nil];        [UIView setanimationduration:0.35]; Nsinteger Selectcol = Sender.selectedsegmentindex + 2;   [Self changepointofimage:selectcol]; [UIView commitanimations];} @end

Bug Description:

When you add an expression and then sort it, you'll find that the Add button goes to the front.

cause: Because Subviews is a nsarray, the added expression is automatically added to the back of Subviews, that is, the Add button is not at the top, that is, the end of Subviews Nsarray. Then, when the loop is sorted, the Add button is always in the tenth position.

Workaround: Place the Add emoticons button at the top level.

In the addfaceimage method, add

[Self.view Bringsubviewtofront:_addimage];
The modified code is as follows:
-(void) addfaceimage {    [UIView beginanimations:nil context:nil];    [UIView setanimationduration:0.35];        _imagecount++;    Nsinteger newindex = _imagecount% 9;    NSString *imagename = [NSString stringwithformat:@ "01%ld.png", NewIndex];        CGFloat x = _addimage.frame.origin.x;    CGFloat y = _addimage.frame.origin.y;        [Self addimage:imagename pointx:x pointy:y];        _col = _imagecount% _columns;     _row = _imagecount/_columns;        CGRect frame = _addimage.frame;    frame.origin.x = _onex + _col * (kimagewidth + _margin);    FRAME.ORIGIN.Y = _oney + _row * (kimagewidth + _margin);    _addimage.frame = frame;        [Self.view Bringsubviewtofront:_addimage];        [UIView commitanimations];}
The effect is as follows:

iOS learning-the arrangement of emoticons

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.