ios18---自定义控件3改进
控制器:
// 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
/**
* 懒加载,get方法
*/
- (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.创建一个商品*****************************/
/*
XMGShopView *shopView = [[XMGShopView alloc] init];
shopView.frame = CGRectMake(x, y, width, height);
// 设置数据
XMGShop *shop = self.dataArr[index];
shopView.shop = shop;
[self.shopCarView addSubview:shopView];
*/ XMGShopView *shopView = [XMGShopView shopViewWithShop:self.dataArr[index]];
shopView.frame = CGRectMake(x, y, width, height);
[self.shopCarView addSubview:shopView]; /***********************3.设置数据*****************************/
// 设置数据
// XMGShop *shop = self.dataArr[index];
// [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>
@class XMGShop; @interface XMGShopView : UIView /** 商品模型 */
@property (nonatomic, strong) XMGShop *shop; // 构造方法
- (instancetype)initWithShop: (XMGShop *)shop;
+ (instancetype)shopViewWithShop: (XMGShop *)shop; @end
// XMGShopView.m #import "XMGShopView.h"
#import "XMGShop.h" @interface XMGShopView ()
/** 图片控件 */
@property (nonatomic, weak) UIImageView *iconView;
/** 标题控件 */
@property (nonatomic, weak) UILabel *titleLabel;
@end @implementation XMGShopView /**
* 初始化子控件(不要设置frame)
*
*/
- (instancetype)init{
if (self = [super init]) {
[self setUp];
}
return self;
} //构造方法,对象方法
- (instancetype)initWithShop:(XMGShop *)shop{
if (self = [super init]) {
// 注意:先创建后赋值,不然空指针,
[self setUp];
self.shop = shop;//调用set方法
}
return self;
}
//构造方法,类方法
+ (instancetype)shopViewWithShop:(XMGShop *)shop{
return [[self alloc] initWithShop:shop];
} /**
* 初始化
*/
- (void)setUp{
// 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;
} /**
* 布局子控件(可以拿到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);
} /**
* 重写set方法:只要外边传数据就会调用
* 作用:设置数据
*/
- (void)setShop:(XMGShop *)shop{
_shop = shop; // 设置数据
self.iconView.image = [UIImage imageNamed:shop.icon];
self.titleLabel.text = shop.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
ios18---自定义控件3改进的更多相关文章
- Qt编写自定义控件大全
最新版可执行文件 http://pan.baidu.com/s/1i491FQP 不定期增加控件及修正BUG和改进算法. 总图: 1:动画按钮 * 1:可设置显示的图像和底部的文字 * 2:可设置普通 ...
- Qt编写自定义控件二动画按钮
现在的web发展越来越快,很多流行的布局样式,都是从web开始的,写惯了Qt widgets 项目,很多时候想改进一下现有的人机交互,尤其是在现有的按钮上加一些动画的效果,例如鼠标移上去变大,移开还原 ...
- QT自定义控件系列(二) --- Loading加载动画控件
本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...
- Qt自定义控件系列(一) --- 圆形进度条
本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...
- Asp.net自定义控件开发任我行(附1)-属性一览众山小
元数据属性应用于服务器控件及其成员,从而提供由设计工具.ASP.NET 页分析器.ASP.NET 运行库以及公共语言运行库使用的信息.当页开发人员在可视化设计器中使用控件时,设计时属性能改进开发人员的 ...
- Asp.net自定义控件开发任我行(2)-TagPrefix标签
摘要 前面我们已经做了一个最简单的TextBox的马甲,此篇文章,我们来讲讲自定义控件的标签.大家可能看到了上一篇中拖放进来的代码是 <cc1:TextEdit ID="TextEdi ...
- Qt编写自定义控件63-水波效果
一.前言 几年前就一直考虑过写这个控件了,在9年前用C#的时候,就看到过别人用C#写了个水波效果的控件,挺好玩的,当时看了下代码用的二维数组来存储变换的图像像素数据,自从学了Qt以后,有过几次想要用Q ...
- C#用户自定义控件(含源代码)-透明文本框
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...
- WPF教程十二:了解自定义控件的基础和自定义无外观控件
这一篇本来想先写风格主题,主题切换.自定义配套的样式.但是最近加班.搬家.新租的房子打扫卫生,我家宝宝6月中旬要出生协调各种的事情,导致了最近精神状态不是很好,又没有看到我比较喜欢的主题风格去模仿的, ...
- android自定义控件一站式入门
自定义控件 Android系统提供了一系列UI相关的类来帮助我们构造app的界面,以及完成交互的处理. 一般的,所有可以在窗口中被展示的UI对象类型,最终都是继承自View的类,这包括展示最终内容的非 ...
随机推荐
- CSU1019: Simple Line Editor
1019: Simple Line Editor Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Subm ...
- 零基础入门学习Python(1)--我和Python的第一次亲密接触
前言 最近在学习Python编程语言,于是乎就在网上找资源.其中小甲鱼<零基础入门学习Python>试听了几节课,感觉还挺不错,里面的视频都是免费下载,小甲鱼讲话也挺幽默风趣的,所以呢,就 ...
- 基于nvm的Node、NPM的版本管理(NPM permission error的解决)
最近在使用npm过程中,发现全局安装总会遇到permission相关的错误,所以总是要在前面加sudo,还得不停输入密码. 懒惰使我进步,随手google了一下相关问题的解决方案,发现npm在官方文档 ...
- Redis 压缩存储的配置
如题,redis是采用了ziplist 元素在不足一定数量时采用压缩存储 hash: zset: list: 如上图所示: ziplist-entries:最大元素数量(即存储了多少个元素) zipl ...
- Shrio Demo
package com.atguigu.shiro.helloworld; import org.apache.shiro.SecurityUtils; import org.apache.shiro ...
- prometheus监控mysql
创建一个用于mysqld_exporter连接到MySQL的用户并赋予所需的权限 mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO '; my ...
- Django——配置服务器上线
使用UWSGI和NGINX配置项目上线 首先你得有一个拿得出手的项目 其次,购买了域名,也备案成功了 将settings.py中的DEBUG设置为False 配置Uwsgi 在项目(哪里都可以)中创建 ...
- Win 2003 创建 IP 安全策略来屏蔽端口的图文教程
(本文用示例的方法讲解 IP 安全策略的设置方法,具体的设置还是要根据个人实际的需要来设置.另外 Windows Server 2008 与此类似.千一网络编辑注) IP安全性(Internet Pr ...
- 【转】Java读写文件大全
使用Java操作文本文件的方法详解 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(ch ...
- 删除右键open foler as pycharm project(WIN10)
1.打开注册表(WIN+R 输入regedit) 2.找到 HKEY_CLASSES_ROOT\Directory\Background 路径 下找到Parcharm文件夹,删除,右键的open fo ...