• ios UI 图片排列(简单实现)


     1 //
     2 //  ViewController.h
     3 //  06-imgPlace
     4 //
     5 //  Created by zjj on 15/5/7.
     6 //  Copyright (c) 2015年 zjj. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @interface ViewController : UIViewController
    12 
    13 - (IBAction)indexChange:(UISegmentedControl *)sender;
    14 
    15 @end
      1 //
      2 //  ViewController.m
      3 //  06-imgPlace
      4 //
      5 //  Created by zjj on 15/5/7.
      6 //  Copyright (c) 2015年 zjj. All rights reserved.
      7 //
      8 
      9 #import "ViewController.h"
     10 
     11 #define kImgw 50// 宏定义常量格式 #define x 40等价int x =40 全局变量
     12 #define kImgh 50
     13 
     14 @interface ViewController ()
     15 {
     16     NSArray *array;
     17 
     18 }
     19 @end
     20 
     21 @implementation ViewController
     22 #pragma mark 控制器的view加载完毕就调用一次img
     23 - (void)viewDidLoad {
     24     [super viewDidLoad];
     25     array =@[
     26                       @"010.png",
     27                       @"011.png",
     28                       @"012.png",
     29                       @"013.png",
     30                       @"014.png",
     31                       @"015.png",
     32                       @"016.png",
     33                       @"017.png",
     34                       @"018.png",
     35                       ];
     36     int  columns = 2;
     37     //每个表情之间的间距 = (控制器的宽度-列数*表情宽度)/(列数+1)
     38     CGFloat margin = (self.view.frame.size.width - columns * kImgw) / (columns + 1);
     39     // 定义第一个表情的x值
     40     CGFloat oneX = 100;
     41     //y值
     42     CGFloat oneY = margin;
     43     for (int i = 0; i < array.count; i++) {
     44         //        int no = i % array.count;//取余 控制数组越界 【0,8】size=9
     45         // i这个位置对应的列数
     46         int col = i % columns;
     47         // i这个位置对应的行数
     48         int row = i / columns;
     49         CGFloat x = oneX + col * (kImgh + margin);
     50         CGFloat y = oneY + row * (kImgw + margin);
     51         [self addImg:array[i] x:x y:y];
     52     }
     53     
     54 }
     55 
     56 
     57 - (void)didReceiveMemoryWarning {
     58     [super didReceiveMemoryWarning];
     59     // Dispose of any resources that can be recreated.
     60 }
     61 - (void)addImg :(NSString *)icon x:(CGFloat) x y:(CGFloat) y
     62 {
     63     UIImageView *one =  [[UIImageView alloc]init];
     64     one.image = [UIImage imageNamed:icon];
     65     one.frame = CGRectMake(x, y, kImgw, kImgh);
     66     [self.view addSubview:one];
     67 }
     68 
     69 #pragma mark 更改表情的列数
     70 - (IBAction)indexChange:(UISegmentedControl *)sender {
     71     [UIView beginAnimations:nil context:nil];
     72     [UIView setAnimationDuration:0.5];
     73     int columns =  (int)sender.selectedSegmentIndex + 2;//确定列数
     74     //每个表情之间的间距 = (控制器的宽度-列数*表情宽度)/(列数+1)
     75     CGFloat margin = (self.view.frame.size.width - columns * kImgw) / (columns + 1);
     76     // 定义第一个表情的x值
     77     CGFloat oneX = 50;
     78     //y值
     79     CGFloat oneY = margin;
     80     for (int i = 0; i < array.count; i++) {
     81         // int no = i % array.count;//取余 控制数组越界 【0,8】size=9
     82         // i这个位置对应的列数
     83         int col = i % columns;
     84         // i这个位置对应的行数
     85         int row = i / columns;
     86         CGFloat x = oneX + col * (kImgh + margin);
     87         CGFloat y = oneY + row * (kImgw + margin);
     88 //        [self addImg:array[i] x:x y:y]; // 每次点击按钮都要创建函数不可取
     89         // 取出+1 为hi对应的imgview,设置x和y
     90         // +1是为了跳过最前面的UISegmentedControl
     91         UIView *child = self.view.subviews[i + 1];
     92 //        NSLog(@"%@",child.class);
     93         CGRect tempF = child.frame;
     94 //        tempF.origin.x = x;
     95 //        tempF.origin.y = y;//两句等价于CGPointMake
     96         tempF.origin = CGPointMake(x, y);
     97         child.frame =tempF;
     98         [UIView commitAnimations];
     99     }
    100     NSLog(@"打印子控件数量%ld",self.view.subviews.count);
    101 }

    效果图

    代码重构

     1 //
     2 //  ViewController.m
     3 //  06-imgPlace
     4 //
     5 //  Created by zjj on 15/5/7.
     6 //  Copyright (c) 2015年 zjj. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 // 宏定义常量格式 #define x 40等价int x =40 全局变量
    11 #define kImgw 25 // 图标宽度
    12 #define kImgh 25 // 图标高度
    13 #define kPointX 0 //第一个图标坐标x值
    14 @interface ViewController ()
    15 {
    16     NSArray *array;// 定义全局可用
    17 }
    18 @end
    19 
    20 @implementation ViewController
    21 #pragma mark 控制器的view加载完毕就调用一次img
    22 - (void)viewDidLoad {
    23     [super viewDidLoad];
    24     // 创建图片地址数组
    25     array =@[
    26               @"010.png",
    27               @"011.png",
    28               @"012.png",
    29               @"013.png",
    30               @"014.png",
    31               @"015.png",
    32               @"016.png",
    33               @"017.png",
    34               @"018.png",
    35             ];
    36     [self adjustImgPostWithColumns:2 add:YES];
    37 }
    38 #pragma 代码重构
    39 - (void) adjustImgPostWithColumns :(int)columns add:(BOOL)add
    40 {
    41 //#warning 不同地方标记一下 已修改
    42     //每个表情之间的间距 = (控制器的宽度-列数*表情宽度)/(列数+1)
    43     CGFloat margin = (self.view.frame.size.width - columns * kImgw) / (columns + 1);
    44     // 定义第一个表情的x值
    45     CGFloat oneX = kPointX;
    46     // 定义第一个表情的y值
    47     CGFloat oneY = margin;
    48     // 创建所有的表情
    49     for (int i = 0; i < array.count; i++) {
    50         // i这个位置对应的列数
    51         int col = i % columns;
    52         // i这个位置对应的行数
    53         int row = i / columns;
    54         CGFloat x = oneX + col * (kImgh + margin);
    55         CGFloat y = oneY + row * (kImgw + margin);
    56 //#warning 创建方式不同 需要重构  
    57         if(add)
    58         {
    59             [self addImg:array[i] x:x y:y];
    60         }
    61         else
    62         {
    63             // 取出+1 为hi对应的imgview,设置x和y
    64             // +1是为了跳过最前面的UISegmentedControl
    65             UIView *child = self.view.subviews[i + 1];
    66             CGRect tempF = child.frame;
    67             tempF.origin = CGPointMake(x, y);
    68             child.frame =tempF;
    69         }
    70     }
    71 }
    72 #pragma mark 添加表情 icon表情图片名
    73 - (void)addImg :(NSString *)icon x:(CGFloat) x y:(CGFloat) y
    74 {
    75     UIImageView *one =  [[UIImageView alloc]init];
    76     one.image = [UIImage imageNamed:icon];
    77     one.frame = CGRectMake(x, y, kImgw, kImgh);
    78     [self.view addSubview:one];
    79 }
    80 
    81 #pragma mark 更改表情的列数
    82 - (IBAction)indexChange:(UISegmentedControl *)sender {
    83     [UIView beginAnimations:nil context:nil];
    84     [UIView setAnimationDuration:0.5];
    85     [self adjustImgPostWithColumns:(int)sender.selectedSegmentIndex + 2 add:NO];
    86     [UIView commitAnimations];
    87 //    NSLog(@"%f - %f",self.view.frame.size.width,self.view.frame.size.height);
    88 //    NSLog(@"打印子控件数量%ld",self.view.subviews.count);
    89 }
    90 @end
  • 相关阅读:
    sed
    UCOSIII(二)
    UCOSIII(一)
    IIC
    SPI
    vii
    find
    grep
    Scrum项目4.0
    Scrum项目3.0
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4485236.html
Copyright © 2020-2023  润新知