便利的初始化view以及设置tag值
便利的初始化view以及设置tag值

效果

源码
https://github.com/YouXianMing/iOS-Project-Examples 中的 SetRect
//
// AccessViewTagProtocol.h
// Animations
//
// Created by YouXianMing on 16/6/17.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> typedef void(^ViewSetupBlock)(UIView * view); @protocol AccessViewTagProtocol <NSObject> @required /**
* Set the view whose tag matches the specified value.
*
* @param view View.
* @param tag tag.
*/
- (void)setView:(UIView *)view withTag:(NSInteger)tag; /**
* Remove the tag.
*
* @param tag View's tag.
*/
- (void)removeReferenceWithTag:(NSInteger)tag; /**
* Get the view from the tag.
*
* @param tag.
*
* @return view's object.
*/
- (id)viewWithTag:(NSInteger)tag; @end
//
// UIView+FrameAndTag.h
// SetRect
//
// Created by YouXianMing on 16/6/19.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
#import "AccessViewTagProtocol.h" @interface UIView (FrameAndTag) <AccessViewTagProtocol> #pragma mark - Set tags. /**
* Support AccessViewTagProtocol.
*/
- (void)supportAccessViewTagProtocol; /**
* Get the view with specified tag from CustomViewController type's controller.
*
* @param tag View's tag.
* @param object AccessViewTagProtocol's object.
*
* @return The view.
*/
+ (instancetype)viewWithTag:(NSInteger)tag from:(id <AccessViewTagProtocol>)object; /**
* Set the view's tag.
*
* @param tag View's tag.
* @param object AccessViewTagProtocol's object.
*/
- (void)setTag:(NSInteger)tag attachedTo:(id <AccessViewTagProtocol>)object; #pragma mark - Init frames. /**
* 设置尺寸以及设置tag值
*/
+ (instancetype)viewWithFrame:(CGRect)frame insertIntoView:(UIView *)view tag:(NSInteger)tag
attachedTo:(id <AccessViewTagProtocol>)object setupBlock:(ViewSetupBlock)block; /**
* 设置尺寸
*/
+ (instancetype)viewWithFrame:(CGRect)frame insertIntoView:(UIView *)view setupBlock:(ViewSetupBlock)block; #pragma mark - Init line view. /**
* 水平线条
*/
+ (instancetype)lineViewInsertIntoView:(UIView *)view positionY:(CGFloat)positionY thick:(CGFloat)thick
leftGap:(CGFloat)leftGap rightGap:(CGFloat)rightGap color:(UIColor *)color; /**
* 垂直线条
*/
+ (instancetype)lineViewInsertIntoView:(UIView *)view positionX:(CGFloat)positionX thick:(CGFloat)thick
topGap:(CGFloat)topGap bottomGap:(CGFloat)bottomGap color:(UIColor *)color; @end NS_INLINE id viewFrom(id <AccessViewTagProtocol> object, NSInteger tag) { return [UIView viewWithTag:tag from:object];
}
//
// UIView+FrameAndTag.m
// SetRect
//
// Created by YouXianMing on 16/6/19.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <objc/runtime.h>
#import "UIView+FrameAndTag.h" @interface UIView () @property (nonatomic, strong) NSNumber *tagNumberValue;
@property (nonatomic, strong) NSMapTable <NSString *, UIView *> *viewsWeakMap; @end @implementation UIView (FrameAndTag) - (void)supportAccessViewTagProtocol { self.viewsWeakMap = [NSMapTable strongToWeakObjectsMapTable];
} + (instancetype)viewWithTag:(NSInteger)tag from:(id <AccessViewTagProtocol>)object { return [object viewWithTag:tag];
} - (void)setTag:(NSInteger)tag attachedTo:(id <AccessViewTagProtocol>)object { self.tagNumberValue ? [object removeReferenceWithTag:self.tagNumberValue.integerValue] : ;
self.tag = tag;
self.tagNumberValue = @(tag);
[object setView:self withTag:tag];
} + (instancetype)viewWithFrame:(CGRect)frame
insertIntoView:(UIView *)view
tag:(NSInteger)tag
attachedTo:(id <AccessViewTagProtocol>)object
setupBlock:(ViewSetupBlock)block { UIView *tmpView = [[[self class] alloc] initWithFrame:frame];
[tmpView supportAccessViewTagProtocol]; view && [view isKindOfClass:[UIView class]] ? ([view addSubview:tmpView]) : ;
object && [object respondsToSelector:@selector(setView:withTag:)] ? ([tmpView setTag:tag attachedTo:object]) : ; if (block) { block(tmpView);
} return tmpView;
} + (instancetype)viewWithFrame:(CGRect)frame
insertIntoView:(UIView *)view
setupBlock:(ViewSetupBlock)block { UIView *tmpView = [[[self class] alloc] initWithFrame:frame];
[tmpView supportAccessViewTagProtocol]; view && [view isKindOfClass:[UIView class]] ? ([view addSubview:tmpView]) : ; if (block) { block(tmpView);
} return tmpView;
} + (instancetype)lineViewInsertIntoView:(UIView *)view positionY:(CGFloat)positionY thick:(CGFloat)thick
leftGap:(CGFloat)leftGap rightGap:(CGFloat)rightGap color:(UIColor *)color { UIView *tmpView = [[[self class] alloc] initWithFrame:CGRectMake(leftGap, positionY, view.frame.size.width - leftGap - rightGap, thick)];
color ? tmpView.backgroundColor = color : ;
[view addSubview:tmpView]; return tmpView;
} + (instancetype)lineViewInsertIntoView:(UIView *)view positionX:(CGFloat)positionX thick:(CGFloat)thick
topGap:(CGFloat)topGap bottomGap:(CGFloat)bottomGap color:(UIColor *)color { UIView *tmpView = [[[self class] alloc] initWithFrame:CGRectMake(positionX, topGap, thick, view.frame.size.height - topGap - bottomGap)];
color ? tmpView.backgroundColor = color : ;
[view addSubview:tmpView]; return tmpView;
} #pragma mark - Runtime property. - (void)setTagNumberValue:(NSNumber *)tagNumberValue { objc_setAssociatedObject(self, @selector(tagNumberValue), tagNumberValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)tagNumberValue { return objc_getAssociatedObject(self, _cmd);
} - (void)setViewsWeakMap:(NSMapTable<NSString *,UIView *> *)viewsWeakMap { objc_setAssociatedObject(self, @selector(viewsWeakMap), viewsWeakMap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSMapTable<NSString *,UIView *> *)viewsWeakMap { return objc_getAssociatedObject(self, _cmd);
} #pragma mark - AccessViewTagProtocol. - (void)setView:(UIView *)view withTag:(NSInteger)tag { [self.viewsWeakMap setObject:view forKey:@(tag).stringValue];
} - (id)viewWithTag:(NSInteger)tag { return [self.viewsWeakMap objectForKey:@(tag).stringValue];
} - (void)removeReferenceWithTag:(NSInteger)tag { [self.viewsWeakMap removeObjectForKey:@(tag).stringValue];
} @end
细节
需要实现协议(用NSMapTable的strongToWeakObjectsMapTable来作为存储string - view)

获取tag更为便利,不依赖于从哪一个view中获取view,而是直接从NSMapTable中获取

便利的初始化view以及设置tag值的更多相关文章
- View可以设置tag携带数据
View可以设置tag携带数据. 例子 初始化:ImageView iv_brand2 设置:iv_brand2.setTag(strB ...
- c#为字段设置默认值,以及构造函数初始化List对象。
1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...
- 结构体成员管理AVClass AVOption之2AVOption,设置选项值
AVOption用于在FFmpeg中描述结构体中的成员变量.一个AVOption可以包含名称,简短的帮助信息,取值等. 上篇文章中概括了AVClass,AVOption和目标结构体之间的关系.以AVF ...
- 查看特定View的默认属性值
当我在分析focus.touch事件处理代码时发现,有些属性对代码的逻辑有非常重要的影响,比如clickable.focusable 这些属性.这时我们自然而然的想到,那么这些属性的默认值是什么呢?在 ...
- [前端_EasyUI]给easyui的datebox设置默认值,获取不到 的解决方法
//给eayui datebox设置初始值 $("#ctime").datebox("setValue", function(){ var date = new ...
- Browser设置UA值
SWE Browser中的OptionMenu是Controller通过onKeyDown监听KEYCODE_MENU来显示的 public boolean onKeyDown(int keyCode ...
- android view的setVisibility方法值的意思
android view的setVisibility方法值的意思 有三个值 visibility One of VISIBLE, INVISIBLE, or GONE. 常量值为0,意思是可见的 常 ...
- Spring读书笔记-----Spring的Bean之设置Bean值
[java] view plaincopyprint? Java实例的属性值可以有很多种数据类型.基本类型值.字符串类型.java实例甚至其他的Bean实例.java集合.数组等.所以Spring允许 ...
- View Components as Tag Helpers,离在线模板编辑又进一步
在asp.net core mvc中增加了ViewComponent(视图组件)的概念,视图组件有点类似部分视图,但是比部分视图功能更加强大,它更有点像一个控制器. 使用方法 1,定义类派生自View ...
随机推荐
- rabbitmq route
AMQP AMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Server(broker): 接受客户端连接,实现AMQP消息队列和路由功能的进程 ...
- Ubuntu服务器上相关软件或应用时常打不开的问题
于接触linux系统时间不就,所以在操作上难免会出现失误,以下两个问题就是近期经常出现的问题,具体如下: 1.ubuntu服务器上的浏览器时常打不开. 2.安装的pycharm和系统自带的pychar ...
- ubuntun 18.04 desktop安装jupyter-notebook
在ubuntu18.04要安装jupyter-notebook,当然前提是先安装python,然后按如下步骤安装jupyter-notebook,现在记录如下: 1.sudo apt-get upda ...
- 【教程】Tomcat 的catalina.out 日志按照自定义日期格式进行切割
本文简单介绍在使用cronolog对tomcat的日志进行自定义日期格式的切割,方便日志的整理和遇到问题日志的排查! 安装cronolog 安装cronolog的方法网上有很多,这里也简单的介绍一下. ...
- 异常日志框架Exceptionless结合.NET Core(本地部署)
一.前言 1.分布式异常日志收集框架Exceptionless是开源的工具,根据官方给出的说明: Exceptionless可以为您的ASP.NET.Web API.WebFrm.WPF.控制台和MV ...
- js中函数的参数传递
js中所有函数的参数传递都是按值传递,也就是说把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另一个变量一样,基本类型值的传递如同基本类型变量的复制一样,而引用类型的值的传递则如同引用类型 ...
- CF 494 F. Abbreviation(动态规划)
题目链接:[http://codeforces.com/contest/1003/problem/F] 题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空 ...
- 清北学堂省选刷题冲刺班 Test Day3
目录 2018.3.27 Test T1 T2 T3 考试代码 T2 T3 2018.3.27 Test 时间:8:00~11:30 期望得分:100+60+25=185 实际得分:100+40+25 ...
- 利用cve-2017-11882的一次渗透测试
利用工具:https://github.com/Ridter/CVE-2017-11882/ 影响版本: office 2003 office 2007 office 2010 office 2013 ...
- VM 虚拟机网络配置
VM网络设置,一共有四种模式. 分别是 1:bridge:桥接,直接和真实网卡相连.如果你要让虚拟机也要上网,就必须选这项,并且要配置和真实网卡在同一网段的IP地址. 2:host-only: 仅主机 ...