zoukankan      html  css  js  c++  java
  • 九宫格计算思路

    在同一行不断添加View
    Y值不变,X值:(间距+宽度) * (下标 % 总列数)
    0%3 * (hMargin + width) 1%3 * (hMargin + width) 2%3 * (hMargin + width)
    3%3 * (hMargin + width) 4%3 * (hMargin + width) 5%3(hMargin + width)
    6%3 * (hMargin + width) 7%3 * (hMargin + width)

    在不同行不断添加View
    Y值(间距 + 高度) * (下标/总列数),X值:(间距+宽度) * (下标 % 总列数)
    0 1 2->y : / 3 * (vMargin + height) = 0
    3 4 5->y : / 3 * (vMargin + height) = 1
    6 7 8->y : / 3 * (vMargin + height) = 2

    #import "ViewController.h"
    
    @interface ViewController ()
    
    // 购物车
    @property (weak, nonatomic) IBOutlet UIView *shopCarView;
    // 添加按钮
    @property (weak, nonatomic) IBOutlet UIButton *addButton;
    // 删除按钮
    @property (weak, nonatomic) IBOutlet UIButton *removeButton;
    // 全局的下标
    //@property (nonatomic, assign) NSInteger index;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // 给下标赋值
    //    self.index = 0;
        
    //    // 裁剪多余部分(不可取)
    //    self.shopCarView.clipsToBounds = YES;
    }
    
    /**
     *  添加到购物车
     *
     *  @param button 按钮
     */
    - (IBAction)add:(UIButton *)button {
    /***********************1.定义一些常量*****************************/
        // 1.总列数
        NSInteger allCols = 3;
        // 2.商品的宽度 和 高度
        CGFloat width = 80;
        CGFloat height = 100;
        // 3.求出水平间距 和 垂直间距
        CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width) / (allCols -1);
        CGFloat vMargin = (self.shopCarView.frame.size.height - 2 * height) / 1;
        // 4. 设置索引
        NSInteger index = self.shopCarView.subviews.count;
        // 5.求出x值
        CGFloat x = (hMargin + width) * (index % allCols);
        CGFloat y = (vMargin + height) * (index / allCols);
        
    /***********************2.创建一个商品*****************************/
      // 1.创建商品的view
        UIView *shopView = [[UIView alloc] init];
      // 2.设置frame
        shopView.frame = CGRectMake(x, y, width, height);
      // 3.设置背景颜色
        shopView.backgroundColor = [UIColor greenColor];
      // 4.添加到购物车
        [self.shopCarView addSubview:shopView];
        
    /***********************3.设置按钮的状态*****************************/
    //    if (index == 5) {
    //        button.enabled = NO;
    //    }
        button.enabled = (index != 5);
        
        // 5.设置删除按钮的状态
        self.removeButton.enabled = YES;
        
        // 让下标+1
    //    self.index += 1;
    }
    
    /**
     *  从购物车中删除
     *
     *  @param button 按钮
     */
    - (IBAction)remove:(UIButton *)button {
        // 1. 删除最后一个商品
        UIView *lastShopView = [self.shopCarView.subviews lastObject];
        [lastShopView removeFromSuperview];
        
        // 2.设置索引值 -1
    //    self.index -= 1;
        
        // 3. 设置添加按钮的状态
        self.addButton.enabled = YES;
        
        // 4. 设置删除按钮的状态
        /*
        if (self.shopCarView.subviews.count == 0) {
            self.removeButton.enabled = NO;
        }
         */
        self.removeButton.enabled = (self.shopCarView.subviews.count != 0);
        
    }
    @end
  • 相关阅读:
    进程和线程
    堡垒机初识--paramiko模块
    python三元运算
    python 遍历文件夹
    CentOs7安装rabbitmq
    logstash 中配置GeoIP解析地理信息
    Centos7单机部署ELK
    Nginx修改access.log日志时间格式
    nginx日志增加cookie信息
    socketserver多线程处理
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6089843.html
Copyright © 2011-2022 走看看