Development of custom controls for iPhone controls: re-painting or combination

Source: Internet
Author: User

Some custom controls are frequently needed during iPhone development. Of course, for cost and quality considerations, select system controls first. When you really need to develop custom controls, there are usually two options: re-painting or combination. Re-painting is to override the draw function of the control and draw the expected effect in the function. The combination is to form a control that meets the needs through the combination of different controls. Of course, the requirements for custom controls are different. Some are suitable for repainting, some are suitable for combination, and sometimes both are applicable. For example, star controls are commonly used in projects but are not provided by the system. How should we choose this control?

 

 

To solve this problem, we decided to implement it in two ways:

Redraw implementation:

@ Interface starrating: uiview {

Int numberstars;

Int rating;

}

@ Property int numberstars;

@ Property int rating;

-(ID) initwithnumberofstars :( INT) numstars;

-(ID) initwithnumberofstars :( INT) numstars initialrating :( INT) initialrating;

@ End

@ Implementation starrating

-(Void) drawrect :( cgrect) rect {

Uiimage * onimage = [uiimage imagenamed: @ "on.png"];

Uiimage * offimage = [uiimage imagenamed: @ "off.png"];

For (INT I = 1; I & lt; = [self numberstars]; I ++ ){

Uiimage * IMG = (I & lt; = [Self Rating])? Onimage: offimage;

Cgpoint imagepoint;

Imagepoint. x = btn_width * (I-1 );

Imagepoint. Y = 0.0f;

[Img drawatpoint: imagepoint];

}

}

@ Endpoint: Click Upload ratingto select an image and use on.png to download the image. Combined implementation:

@ Interface starrating: uicontrol {

Nsmutablearray * stararray;

Nsinteger numberstars;

Nsinteger rating;

}

-(ID) initwithframe :( cgrect) frame andstars :( nsinteger) innumstars;

@ End

@ Implementation starrating

-(ID) initwithframe :( cgrect) frame andstars :( INT) innumstars {

If (Self = [Super initwithframe: frame])

{

Approxmode = interpolationmode_half;

Starnum = innumstars;

[Self preparestars: starnum frame: frame];

}

Return self;

}

-(Void) setrating :( INT) arating {

Rating = arating;

[Self fillstars];

}

-(Void) preparestars: (INT) astarnum frame: (cgrect) frame {

Stararray = [[nsmutablearray alloc] initwithcapacity: astarnum];

Float width = frame. Size. width/astarnum;

Float Height = frame. Size. height;

For (INT I = 0; I & lt; astarnum; I ++ ){

Uiimageview * star = [[uiimageview alloc] initwithframe: cgrectmake (0 + (I * width), 0, width, height)];

[Stararray addobject: Star];

[Self addsubview: Star];

[Star release];

}

}

-(Void) fillstars {

Uiimage * onimage = [uiimage imagenamed: @ "on.png"];

Uiimage * offimage = [uiimage imagenamed: @ "off.png"];

For (INT I = 1; I & lt; = [self numberstars]; I ++ ){

Uiimage * IMG = (I & lt; = [Self Rating])? Onimage: offimage;

Uiimageview * star = (uiimageview *) [self subviewatindex: I];

[Star setimage: img];

}

}

Five sub-Controls of uiimageview are created first, and then the image attributes of the sub-control are set based on the star rating.

 

Comparison:

The 1 re-painting scheme only has one control, while the combined implementation contains five sub-controls. Resource and efficiency re-painting solutions will be advantageous.

2 re-painting needs to be processed, while the combination is described by the Child control. Therefore, the combination scheme is relatively simple in terms of descriptive processing.

3. When a requirement is changed, the re-drawing scheme may need to modify the descriptive code, while the combination scheme can easily replace the child control type when the design is reasonable.

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.