IOS: Making nine Gongge

Source: Internet
Author: User

Make a simple nine Gongge:

The source code is as follows:

 1 #import "ViewController.h" 2 3 @interface Viewcontroller () 4 5 @end 6 7 @implementation Viewcontroller 8 9-(void  ) Viewdidload {Ten [Super Viewdidload];11 12//create nine Nsinteger col = 3;     Number of rows per row Nsinteger total = 9;//15 16//width of grid, height UIImage *image = [UIImage imagenamed:@ "0.png"];18 CGFloat cellwidth = image.size.width;19 cgfloat cellheight = image.size.height;20 cgfloat screenWidth = SE lf.view.frame.size.width;22 cgfloat gapx = (screenwidth-cellwidth*col)/(col+1),//x direction interval cgfloat GapY = GA         px;//y direction Interval 25 26 27//Count rows Nsinteger row = 0;29 for (int i=0; i<total; i++) 31 {32 UIButton *button = [[UIButton alloc]init];33 [button setbackgroundimage:[uiimage imagenamed:[nsstring stringwit hformat:@ "%d.png", I]] forstate:uicontrolstatenormal];34 35//calculates if a newline is required (I%col = = 0) 37 { row++;39}40 cgfloat x = gapx+(CELLWIDTH+GAPX) * (i%col); CGFloat y = gapy+ (cellheight+gapy) *row;42 button.frame = CGRectMake (x, Y, cell Width, cellheight); [Self.view addsubview:button];45}46}47 @end

IOS: Making nine grid

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.