ios16--自定义控件1
k控制器:
//
// 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 self.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]; /***********************3.设置数据*****************************/
// 设置数据
XMGShop *shop = self.dataArr[index];
shopView.iconView.image = [UIImage imageNamed:shop.icon];
shopView.titleLabel.text = 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
/** 图片控件 */
@property (nonatomic, weak) UIImageView *iconView;// [self addSubview:iconView];已经有强指针引用了,这里用weak
/** 标题控件 */
@property (nonatomic, weak) UILabel *titleLabel;
@end
//
// XMGShopView.m #import "XMGShopView.h" @interface XMGShopView () @end @implementation XMGShopView /**
* 初始化子控件(不要设置frame,获取的宽度高度是0,在layoutSubviews可以拿到)
*
*/
- (instancetype)init{
if (self = [super init]) {
/*
// 0.获取当前控件的尺寸
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height; // NSLog(@"init:%f----%f", width, height);
*/
// 1.创建商品的UIImageView对象
UIImageView *iconView = [[UIImageView alloc] init];
// iconView.frame = CGRectMake(0, 0, width, width);
iconView.backgroundColor = [UIColor blueColor];
[self addSubview:iconView];
self.iconView = iconView; // 2.创建商品标题对象
UILabel *titleLabel = [[UILabel alloc] init];
// titleLabel.frame = CGRectMake(0, width, width, height - width);
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
[self addSubview:titleLabel];//强指针引用了
self.titleLabel = titleLabel;//titleLabel变成全局的,不然layoutSubviews()方法里面获取不到,
}
return self;
} /**
* 布局子控件(可以拿到frame)
*/
- (void)layoutSubviews{
// 0.一定要调用super,保留父类系统的布局。否则父类的布局就没了。
[super layoutSubviews]; // 1.获取当前控件的尺寸
CGFloat width = self.frame.size.width;//这个frame是从外面shopView.frame = CGRectMake(x, y, width, height);获取的
CGFloat height = self.frame.size.height; // 2.设置子控件的frame
self.iconView.frame = CGRectMake(, , width, width);
self.titleLabel.frame = CGRectMake(, width, width, height - width);
} @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
ios16--自定义控件1的更多相关文章
- android自定义控件一站式入门
自定义控件 Android系统提供了一系列UI相关的类来帮助我们构造app的界面,以及完成交互的处理. 一般的,所有可以在窗口中被展示的UI对象类型,最终都是继承自View的类,这包括展示最终内容的非 ...
- ASP.NET MVC学习之母版页和自定义控件的使用
一.母板页_Layout.cshtml类似于传统WebForm中的.master文件,起到页面整体框架重用的目地1.母板页代码预览 <!DOCTYPE html> <html> ...
- C# 自定义控件VS用户控件
1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...
- 自定义控件之 圆形 / 圆角 ImageView
一.问题在哪里? 问题来源于app开发中一个很常见的场景——用户头像要展示成圆的: 二.怎么搞? 机智的我,第一想法就是,切一张中间圆形透明.四周与底色相同.尺寸与头像相同的蒙板图片,盖在 ...
- 如何开发FineReport的自定义控件?
FineReport作为插件化开发的报表软件,有些特殊需求的功能需要自己开发,开发的插件包帆软官方有提提供,可以去帆软论坛上找,本文将主要介绍如何开发一个自定义控件,这里讲讲方法论. 第一步:实例化一 ...
- WPF自定义控件第二 - 转盘按钮控件
继之前那个控件,又做了一个原理差不多的控件.这个控件主要模仿百度贴吧WP版帖子浏览界面左下角那个弹出的按钮盘.希望对大家有帮助. 这个控件和之前的也差不多,为了不让大家白看,文章最后发干货. 由于这个 ...
- 【Win 10应用开发】AdaptiveTrigger在自定义控件中是可以触发的
前些天,看到有网友给我留言,说AdaptiveTrigger在自定义控件(模板化控件)中不能触发.因为当时我正在写其他的代码,就没有去做实验来验证,于是我就给这位网友提了使用GotoVisualSta ...
- WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
随机推荐
- ThinkPHP---thinkphp框架介绍
目录: (1)简述: (2)下载: (3)文件结构: (4)部署: (5)细节问题: 主体: (1)简述 ThinkPHP诞生于2006年初,最初叫FSC.于2007年元旦更名为PHP,同时官网上线. ...
- 写给新手的十一条 Docker 守则
很多人最终还是决定使用 Docker 解决问题. Docker 的优点很多,比如: 一体化——将操作系统.库版本.配置文件.应用程序等全部打包装在容器里.从而保证 QA 所测试的镜像 (image) ...
- <MyBatis>入门八 工作原理
1.获取sqlSessionFactory对象 首先拿到全局配置文件的流对象 创建SqlSessionFactoryBuilder对象,并调用build方法,把流传进去 build方法 创建一个XML ...
- JAVA实现创建Excel表并导出(转发)
<span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...
- buf.readUInt8()
buf.readUInt8(offset[, noAssert]) offset {Number} 0 <= offset <= buf.length - 1 noAssert {Bool ...
- 3. Python中的分支判断、循环
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的分支判断.循环 1. 分支条件判断 1.1 比较操作 以下是数之间常见的比较操作,例如5>3就是数学意义上的比较,5是大 ...
- 洛谷 1017 进制转换 (NOIp2000提高组T1)
[题解] 纯模拟题. 我们都知道十进制数化成m进制数可以用短除法,即除m取余.逆序排列.而m进制数化为十进制数,按权展开求和即可. 但在本题中进制的基数R可能为负数,我们知道a%R的符号与R一致,也就 ...
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
- ZOJ 1516 Uncle Tom's Inherited Land
题目大意: 除去那些作为荷塘的土地块,将剩余的土地希望每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地 合成的长方形土地块 很明显的二分图问题,但是要考虑如何建模 一个长方形土地总是由相邻 ...
- noip模拟赛 街灯
分析:对于前30%的数据直接暴力模拟即可,对于另外30%的数据,因为每次的p是一样的,所以可以用莫队来维护,先离散化一下,再用一个桶统计次数. 100%的做法和之前做过的一道模拟赛题很像,当模数很小的 ...