zoukankan      html  css  js  c++  java
  • IOS:九宫格的实现及添加删除操作

              先上结果图看下

             

             一开始设置两个按钮,其实这两个按钮没必要用懒加载的方式,因为这是一开始就要用或者说常用的东西,直接写比较好。然后提示的label和图片信息的数组,都是该用到的时候才去加载。

             图片我是事先改名1-9了,直接把名字循环存到数组里。然后创建ImageView的时候根据下标取值。

            按钮的点击,会改变下标值。imageView的XY 值根据下标值算的,同时根据index设置自身的标记,删除imageView就是根据这个来。

             imageview的tag,原来我是根据index来的,后来一删除就黑屏,感觉是把self.view移除了。看网上说是viewwithtag会查找自身,估计自身的tag和imageview的重了把。于是我把imageview的tag+10,这样就没问题了

      1 static int arrindex=0;
      2 @interface ViewController ()
      3 @property(strong,nonatomic)UILabel  *itemlabel;
      4 @property(strong,nonatomic) UIButton *addBtn;
      5 @property(strong,nonatomic)  UIButton *removeBtn;
      6 @property(strong,nonatomic)NSMutableArray<UIImage *> *imageArr;
      7 @property(strong,nonatomic)UIImageView *imageView;
      8 @end
      9 
     10 @implementation ViewController
     11 
     12 - (void)viewDidLoad {
     13     [super viewDidLoad];
     14     // Do any additional setup after loading the view.
     15     [self setBaseUI];
     16 //    self.view.backgroundColor=[UIColor redColor];
     17  
     18 }
     19 -(UIButton *)addBtn{
     20     if(!_addBtn){
     21         _addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
     22         _addBtn.frame=CGRectMake(50, 200, 100,40 );
     23         _addBtn.backgroundColor=[UIColor grayColor];
     24         [_addBtn setTitle:@"添加" forState:UIControlStateNormal];
     25          [_addBtn setTitle:@"添加" forState:UIControlStateHighlighted];
     26          [  _addBtn addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
     27         [_addBtn setTag:1];
     28       [self.view  addSubview:_addBtn];
     29     }
     30     return _addBtn;
     31     
     32 }
     33 -(UIButton *)removeBtn{
     34     if(!_removeBtn){
     35         _removeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
     36         _removeBtn.frame=CGRectMake(self.view.bounds.size.width-50-100, 200, 100,40 );
     37         _removeBtn.backgroundColor=[UIColor grayColor];
     38         [_removeBtn setTitle:@"删除" forState:UIControlStateNormal];
     39         [_removeBtn setTitle:@"删除" forState:UIControlStateHighlighted];
     40         [  _removeBtn addTarget:self action:@selector(btn2Click) forControlEvents:UIControlEventTouchUpInside];
     41         [_removeBtn setTag:2];
     42        [self.view  addSubview:_removeBtn];
     43     }
     44     return _removeBtn;
     45     
     46 }
     47 
     48 -(UILabel *)itemlabel{
     49     if(!_itemlabel)
     50     {
     51         _itemlabel=[[UILabel alloc]initWithFrame:CGRectMake(170, 150, 200, 40)   ];
     52         _itemlabel.textColor=[UIColor redColor];
     53         [self.view addSubview:_itemlabel];
     54     }
     55     return _itemlabel;
     56     
     57 }
     58 -(void)setBaseUI{
     59     [self  addBtn];
     60     [self  removeBtn];
     61 }
     62 
     63 -(void)btn1Click{
     64     NSLog(@"点击了添加按钮");
     65     if(arrindex==9)
     66     {
     67         NSLog(@"图片已结束");
     68         self.itemlabel.text=@"图片已结束";
     69        return;
     70     }
     71     self.itemlabel.text=@"";
     72     [self setXY:arrindex:_addBtn];
     73    arrindex=arrindex+1;
     74 }
     75 -(void)btn2Click{
     76     NSLog(@"点击了删除按钮");
     77     if(arrindex==0)
     78     {
     79         NSLog(@"暂无图片");
     80         self.itemlabel.text=@"暂无图片";
     81         return;
     82     }
     83     self.itemlabel.text=@"";
     84     [self setXY:arrindex:_removeBtn];
     85      arrindex=arrindex-1;
     86 }
     87 //将图片存入数组
     88 -(NSMutableArray<UIImage *> *)imageArr{
     89     //将事先的图片信息存入数组里
     90     if(!_imageArr)
     91     {
     92       _imageArr=[NSMutableArray array];
     93     for(int i=0;i<9;i++)
     94         {
     95         NSString  *imageName=[NSString stringWithFormat:@"%d",i+1];
     96         UIImage *image=[UIImage imageNamed:imageName];
     97         [_imageArr addObject:image];
     98         }
     99     }
    100     return _imageArr;
    101 }
    102 
    103 //UIImageView的设定
    104 -(void)imageView:(CGFloat )locx:(CGFloat)locY:(int )index{
    105     CGFloat x=locx;
    106     CGFloat y=locY;
    107     //传入图片的下标值
    108     int arrindex=index;
    109     //坐标传入决定
    110     _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 100, 100)];
    111     _imageView.image=  [self.imageArr objectAtIndex:arrindex];
    112     /*设置imageview的tag,以便后续的删除,删除肯定是要根据标记来。
    113      还有种是self.view.subviews lastobject,但是因为我加了一个label,所以用这个肯定是不太合适的。
    114      */
    115     _imageView.tag=arrindex+10;
    116     _imageView.backgroundColor=[UIColor redColor];
    117     [self.view addSubview:_imageView];
    118     
    119 }
    120 
    121 -(void)setXY:(int )index:(UIButton *)button{
    122     if(button.tag==1)
    123     {
    124     CGFloat viewW=self.view.bounds.size.width;
    125     int row=index/3;
    126     int col=index%3;
    127     /*假如我们设定每个UIimageView的大小是100*100
    128      每行之间的距离为30。第一行从Y-250的位置开始
    129      */
    130     CGFloat space=(viewW-300)/2;
    131     
    132     CGFloat x=col*(space+100);
    133     CGFloat y=250+row*(30+100);
    134     [self imageView:x:y:index];
    135     }
    136     if(button.tag==2)
    137     {
    138         UIImageView *imageview=[self.view viewWithTag:index-1+10 ];
    139         [imageview removeFromSuperview];
    140     }
    141 }
    142 
    143 @end
  • 相关阅读:
    循环的注意点
    c语言实践输出某个区间中不是3的倍数的偶数
    while循环for循环优缺点和应用
    while 和do while循环的区别
    多重if else和switch case的区别
    if else的执行流程
    多个if和一个ifelse的区别
    对两个变量排序,从小到大输出
    【译】第四篇 Integration Services:增量加载-Updating Rows
    【译】第三篇 Integration Services:增量加载-Adding Rows
  • 原文地址:https://www.cnblogs.com/kc1995/p/13679596.html
Copyright © 2011-2022 走看看