控制器:

//
// XMGViewController.h #import <UIKit/UIKit.h> @interface XMGViewController : UIViewController
@end
//
// XMGViewController.m #import "XMGViewController.h"
#import "XMGShop.h"
#import "XMGShopView.h" @interface XMGViewController () @property (weak, nonatomic) IBOutlet UIView *shopCarView;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIButton *removeButton;
@property (nonatomic, strong) NSArray *dataArr; @end @implementation XMGViewController
/**
* 懒加载
*/
- (NSArray *)dataArr{
if (_dataArr == nil) {
// 加载数据
// 1.获取全路径
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil];
self.dataArr = [NSArray arrayWithContentsOfFile:dataPath];
// 字典转模型
// 创建临时数组
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in _dataArr) {
// 创建shop对象
XMGShop *shop = [XMGShop shopWithDict:dict];
// 把模型装入数组
[tempArray addObject:shop];
}
self.dataArr = tempArray;
}
return _dataArr;
} // 初始化数据
- (void)viewDidLoad {
[super viewDidLoad];
} /**
* 添加到购物车
*
* @param button 按钮
*/
- (IBAction)add:(UIButton *)button {
/***********************1.定义一些常量*****************************/
// 1.总列数
NSInteger allCols = ;
// 2.商品的宽度 和 高度
CGFloat width = ;
CGFloat height = ;
// 3.求出水平间距 和 垂直间距
CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width) / (allCols -);
CGFloat vMargin = (self.shopCarView.frame.size.height - * height) / ;
// 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]; // 5.创建商品的UIImageView对象
UIImageView *iconView = [[UIImageView alloc] init];
iconView.frame = CGRectMake(0, 0, width, width);
iconView.backgroundColor = [UIColor blueColor];
[shopView addSubview:iconView]; // 6.创建商品标题对象
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.frame = CGRectMake(0, width, width, height - width);
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
[shopView addSubview:titleLabel];
*/ XMGShopView *shopView = [[XMGShopView alloc] init];
shopView.frame = CGRectMake(x, y, width, height);
[self.shopCarView addSubview:shopView]; // shopView.titleLabel = nil;
/***********************3.设置数据*****************************/
// 设置数据
XMGShop *shop = self.dataArr[index];
/*
shopView.iconView.image = [UIImage imageNamed:shop.icon];
shopView.titleLabel.text = shop.name;
*/
[shopView setIcon:shop.icon];
[shopView setName:shop.name]; /***********************4.设置按钮的状态*****************************/ button.enabled = (index != ); // 5.设置删除按钮的状态
self.removeButton.enabled = YES; } /**
* 从购物车中删除
*
* @param button 按钮
*/
- (IBAction)remove:(UIButton *)button {
// 1. 删除最后一个商品
UIView *lastShopView = [self.shopCarView.subviews lastObject];
[lastShopView removeFromSuperview]; // 3. 设置添加按钮的状态
self.addButton.enabled = YES; // 4. 设置删除按钮的状态
self.removeButton.enabled = (self.shopCarView.subviews.count != ); }
@end

自定义控件:

//
// XMGShopView.h #import <UIKit/UIKit.h> @interface XMGShopView : UIView
// readonly 只会调用get方法
/** 图片控件 */
//@property (nonatomic, weak, readonly) UIImageView *iconView;// [self addSubview:iconView];已经有强指针引用了,这里用weak /** 标题控件 */
//@property (nonatomic, weak, readonly) UILabel *titleLabel; // 提供接口方法
- (void)setIcon: (NSString *)icon;
- (void)setName: (NSString *)name;
@end
//
// XMGShopView.m #import "XMGShopView.h" @interface XMGShopView () //扩展属性
@property (nonatomic, strong) UIImageView *iconView;//[self addSubview:iconView];有强指针指向了,用weak
@property (nonatomic, strong) UILabel *titleLabel;
@end @implementation XMGShopView /**
* 初始化子控件(不要设置frame)
*
*/
- (instancetype)init{
if (self = [super init]) {
// 1.创建商品的UIImageView对象
UIImageView *iconView = [[UIImageView alloc] init];
iconView.backgroundColor = [UIColor blueColor];
[self addSubview:iconView];
_iconView = iconView; // 2.创建商品标题对象
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
[self addSubview:titleLabel];
_titleLabel = titleLabel;
}
return self;
} /**
* 布局子控件(可以拿到frame)
*/
- (void)layoutSubviews{
// 0.一定要调用super
[super layoutSubviews]; // 1.获取当前控件的尺寸
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height; // 2.设置子控件的frame
self.iconView.frame = CGRectMake(, , width, width);
self.titleLabel.frame = CGRectMake(, width, width, height - width);
} - (void)setIcon:(NSString *)icon{
// 设置数据
self.iconView.image = [UIImage imageNamed:icon];
} - (void)setName:(NSString *)name{
// 设置数据
self.titleLabel.text = name;
} @end

bean:

//
// XMGShop.h #import <Foundation/Foundation.h> @interface XMGShop : NSObject /** 图片的名称 */
@property (nonatomic, copy) NSString *icon;
/** 商品的名称 */
@property (nonatomic, copy) NSString *name; // 提供构造方法
/*
- (instancetype)initWithIcon: (NSString *)icon name: (NSString *)name;
+ (instancetype)shopWithIcon: (NSString *)icon name: (NSString *)name;
*/ - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)shopWithDict:(NSDictionary *)dict; @end
//
// XMGShop.m #import "XMGShop.h" @implementation XMGShop
/*
- (instancetype)initWithIcon:(NSString *)icon name:(NSString *)name{
if (self = [super init]) {
self.icon = icon;
self.name = name;
}
return self;
} + (instancetype)shopWithIcon:(NSString *)icon name:(NSString *)name{
return [[self alloc] initWithIcon:icon name:name];
}
*/ - (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.name = dict[@"name"];
}
return self;
} + (instancetype)shopWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
} @end

ios17--自定义控件2改进的更多相关文章

  1. Qt编写自定义控件大全

    最新版可执行文件 http://pan.baidu.com/s/1i491FQP 不定期增加控件及修正BUG和改进算法. 总图: 1:动画按钮 * 1:可设置显示的图像和底部的文字 * 2:可设置普通 ...

  2. Qt编写自定义控件二动画按钮

    现在的web发展越来越快,很多流行的布局样式,都是从web开始的,写惯了Qt widgets 项目,很多时候想改进一下现有的人机交互,尤其是在现有的按钮上加一些动画的效果,例如鼠标移上去变大,移开还原 ...

  3. QT自定义控件系列(二) --- Loading加载动画控件

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  4. Qt自定义控件系列(一) --- 圆形进度条

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  5. Asp.net自定义控件开发任我行(附1)-属性一览众山小

    元数据属性应用于服务器控件及其成员,从而提供由设计工具.ASP.NET 页分析器.ASP.NET 运行库以及公共语言运行库使用的信息.当页开发人员在可视化设计器中使用控件时,设计时属性能改进开发人员的 ...

  6. Asp.net自定义控件开发任我行(2)-TagPrefix标签

    摘要 前面我们已经做了一个最简单的TextBox的马甲,此篇文章,我们来讲讲自定义控件的标签.大家可能看到了上一篇中拖放进来的代码是 <cc1:TextEdit ID="TextEdi ...

  7. Qt编写自定义控件63-水波效果

    一.前言 几年前就一直考虑过写这个控件了,在9年前用C#的时候,就看到过别人用C#写了个水波效果的控件,挺好玩的,当时看了下代码用的二维数组来存储变换的图像像素数据,自从学了Qt以后,有过几次想要用Q ...

  8. C#用户自定义控件(含源代码)-透明文本框

    using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...

  9. WPF教程十二:了解自定义控件的基础和自定义无外观控件

    这一篇本来想先写风格主题,主题切换.自定义配套的样式.但是最近加班.搬家.新租的房子打扫卫生,我家宝宝6月中旬要出生协调各种的事情,导致了最近精神状态不是很好,又没有看到我比较喜欢的主题风格去模仿的, ...

  10. android自定义控件一站式入门

    自定义控件 Android系统提供了一系列UI相关的类来帮助我们构造app的界面,以及完成交互的处理. 一般的,所有可以在窗口中被展示的UI对象类型,最终都是继承自View的类,这包括展示最终内容的非 ...

随机推荐

  1. Java Servlet JSP编程(一)

    最近想学学java编程,java现在的应用还是挺广泛的,有必要学习一下. # index.jsp <%@ page language="java" contentType=& ...

  2. URL解析-URLComponents

    let components = URLComponents(url: fakeUrl, resolvingAgainstBaseURL: false)! http://10.100.140.84/m ...

  3. 手机中快速看图,浏览编辑DWG 梦想极光CAD

    梦想极光CAD6.0(2016.3.1) 手机版最新更新 1.增加手机上,图纸浏览时预览功能 2.增加直接从手机,QQ接收目录下加载文件功能 3.手机交互界面优化 4.增加新建图纸功能 5.增加缓存功 ...

  4. POJ2152 Fire (树形DP)

    题意:n个城市n-1条边 组成一棵树 在每个城市修建消防站会有一个花费costi 每个城市能防火当且仅当地图上距离他最近的消防站距离小于di   问如何修建消防站 使地图上所有的城市都有预防火灾的能力 ...

  5. 官方安装docker-ce步骤

    这里是Centos7安装方式 安装依赖包 $ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 添加Docker ...

  6. idea 快捷键设置

    通过 点击放大镜然后按当前需要修改的快捷键找到需要修改的快捷键,更改成希望的快捷键

  7. 【DIP, 图像增强】

    第四章 图像增强 图像增强是按特定的需要突出一幅图像中的某些信息,同时削弱或者去除某些不需要的信息的处理方法.其主要目的是使处理后的图像对某种特定的应用来说,比原始图像更加适用.因此这类处理是为了某种 ...

  8. 泛型约束 ---类型参数上的限制(where T:class,new())

    今天遇到的一个问题,找到了解决办法,收藏以后记得看一下!

  9. cocos2dx 用命令行进行编译的指令

    cocos compile -p android -m release -s F:\cocosproj\antest --ap android-20 上面那条就是我常用的指令,这指令修改一下就成了你的 ...

  10. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes

                                                        D. Prefixes and Suffixes You have a string s = s ...