便利的初始化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值的更多相关文章

  1. View可以设置tag携带数据

    View可以设置tag携带数据.       例子             初始化:ImageView  iv_brand2              设置:iv_brand2.setTag(strB ...

  2. c#为字段设置默认值,以及构造函数初始化List对象。

    1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...

  3. 结构体成员管理AVClass AVOption之2AVOption,设置选项值

    AVOption用于在FFmpeg中描述结构体中的成员变量.一个AVOption可以包含名称,简短的帮助信息,取值等. 上篇文章中概括了AVClass,AVOption和目标结构体之间的关系.以AVF ...

  4. 查看特定View的默认属性值

    当我在分析focus.touch事件处理代码时发现,有些属性对代码的逻辑有非常重要的影响,比如clickable.focusable 这些属性.这时我们自然而然的想到,那么这些属性的默认值是什么呢?在 ...

  5. [前端_EasyUI]给easyui的datebox设置默认值,获取不到 的解决方法

    //给eayui datebox设置初始值 $("#ctime").datebox("setValue", function(){ var date = new ...

  6. Browser设置UA值

    SWE Browser中的OptionMenu是Controller通过onKeyDown监听KEYCODE_MENU来显示的 public boolean onKeyDown(int keyCode ...

  7. android view的setVisibility方法值的意思

    android view的setVisibility方法值的意思 有三个值 visibility  One of VISIBLE, INVISIBLE, or GONE. 常量值为0,意思是可见的 常 ...

  8. Spring读书笔记-----Spring的Bean之设置Bean值

    [java] view plaincopyprint? Java实例的属性值可以有很多种数据类型.基本类型值.字符串类型.java实例甚至其他的Bean实例.java集合.数组等.所以Spring允许 ...

  9. View Components as Tag Helpers,离在线模板编辑又进一步

    在asp.net core mvc中增加了ViewComponent(视图组件)的概念,视图组件有点类似部分视图,但是比部分视图功能更加强大,它更有点像一个控制器. 使用方法 1,定义类派生自View ...

随机推荐

  1. windows 2008 启用.NET Framework 3.5

    Win2008下已经集成了.NET 3.5.1 framework,需要在管理界面打开! 方法和步骤是: 服务器管理器 -> 功能 -> 添加功能 然后在“选择功能”界面勾选“.NET F ...

  2. #ASP.NET 自制免费.NET代码生成器KevinCodeBuilder

    简单的.NET三层架构会有很多重复的代码,如果手敲会比较浪费时间.前段时间在互联网找了下.NET的代码生成器,发现要么太复杂甚至生成的代码会报错,要么要收费而且效果不是自己想要的. 所以,干脆自己做一 ...

  3. spark streaming限制吞吐

    使用spark.streaming.receiver.maxRate这个属性限制每秒的最大吞吐.官方文档如下: Maximum rate (number of records per second) ...

  4. C#并行编程(6):线程同步面面观

    理解线程同步 线程的数据访问 在并行(多线程)环境中,不可避免地会存在多个线程同时访问某个数据的情况.多个线程对共享数据的访问有下面3种情形: 多个线程同时读取数据: 单个线程更新数据,此时其他线程读 ...

  5. js冲刺一下

    js中__proto__和prototype的区别和关系 1.对象有属性__proto__,指向该对象的构造函数的原型对象. 2.方法除了有属性__proto__,还有属性prototype,prot ...

  6. queue模块回顾

    queue queue是python中的标准库,俗称队列. 在python中,多个线程之间的数据是共享的,多个线程进行数据交换的时候,不能够保证数据的安全性和一致性,所以当多个线程需要进行数据交换的时 ...

  7. linux 驱动之LCD驱动(有framebuffer)

    <简介> LCD驱动里有个很重要的概念叫帧缓冲(framebuffer),它是Linux系统为显示设备提供的一个接口,应用程序在图形模式允许对显示缓冲区进行读写操作.用户根本不用关心物理显 ...

  8. 「PKUWC2018」猎人杀

    「PKUWC2018」猎人杀 解题思路 首先有一个很妙的结论是问题可以转化为已经死掉的猎人继续算在概率里面,每一轮一直开枪直到射死一个之前没死的猎人为止. 证明,设所有猎人的概率之和为 \(W\) , ...

  9. Asyncio中Lock部分的翻译

    Asyncio中Lock部分的翻译 Locks class asyncio.Lock(*, loop=None) 原始锁的对象. 这个基础的锁是一个同步化的组件,当它上锁的时候就不属于典型的协程了(译 ...

  10. Intel P6以来核心架构及对应型号、芯片组一览表

    转载或拿走使用请注明出处,谢谢! 注1:5系列以前的芯片组部分可以支持多代处理器(如部分945可以支持65nm.45nm的处理器),5系列开始此现象较少见. 注2:插座兼容性①Socket370接口处 ...