Convenient initialization of view and setting tag value

Source: Internet
Author: User

Convenient initialization of view and setting tag value

Effect

Source

The SetRect in Https://github.com/YouXianMing/iOS-Project-Examples

////AccessViewTagProtocol.h//Animations////Created by youxianming on 16/6/17.//copyright©2016 year youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>typedefvoid(^viewsetupblock) (UIView *view);@protocolAccessviewtagprotocol <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 year youxianming. All rights reserved.//#import<UIKit/UIKit.h>#import "AccessViewTagProtocol.h"@interfaceUIView (Frameandtag) <AccessViewTagProtocol>#pragmaMark-set tags./** * Support Accessviewtagprotocol.*/- (void) Supportaccessviewtagprotocol;/** Get The view with specified tag from Customviewcontroller type ' s controller. * @param tag View ' tag. * @p Aram 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;#pragmaMark-init frames./** * Set the size and set the tag value*/+ (Instancetype) Viewwithframe: (CGRect) frame Insertintoview: (UIView *) View tag: (nsinteger) tag attachedto: (ID<AccessViewTagProtocol>)ObjectSetupblock: (viewsetupblock) block;/** * Set size*/+ (Instancetype) Viewwithframe: (CGRect) frame Insertintoview: (UIView *) View Setupblock: (viewsetupblock) block;#pragmaMark-init line View./** * Horizontal line*/+ (Instancetype) Lineviewinsertintoview: (UIView *) View Positiony: (cgfloat) positiony thick: (cgfloat) thick leftgap: (cgfloat) Leftgap rightga P: (cgfloat) Rightgap color: (Uicolor*) color;/** * Vertical line*/+ (Instancetype) Lineviewinsertintoview: (UIView *) View Positionx: (cgfloat) Positionx thick: (cgfloat) thick topgap: (cgfloat) Topgap Bottomga P: (cgfloat) Bottomgap color: (Uicolor*) color;@endNs_inlineIDViewfrom (ID<AccessViewTagProtocol>Object, Nsinteger tag) {        return[UIView Viewwithtag:tag from:Object];}
////UIVIEW+FRAMEANDTAG.M//SetRect////Created by youxianming on 16/6/19.//copyright©2016 year youxianming. All rights reserved.//#import<objc/runtime.h>#import "uiview+frameandtag.h"@interfaceUIView () @property (nonatomic, strong) NSNumber*Tagnumbervalue, @property (nonatomic, strong) nsmaptable<nsstring *, UIView *> *Viewsweakmap;@end@implementationUIView (Frameandtag)- (void) Supportaccessviewtagprotocol {self.viewsweakmap=[nsmaptable strongtoweakobjectsmaptable];}+ (Instancetype) Viewwithtag: (Nsinteger) tag from: (ID<AccessViewTagProtocol>)Object {        return[ObjectViewwithtag:tag];}- (void) Settag: (nsinteger) tag attachedto: (ID<AccessViewTagProtocol>)Object{Self.tagnumbervalue? [ObjectRemoveReferenceWithTag:self.tagNumberValue.integerValue]:0; Self.tag=tag; Self.tagnumbervalue=@ (tag); [Objectsetview:self Withtag:tag];}+(Instancetype) Viewwithframe: (CGRect) frame Insertintoview: (UIView*) View tag: (nsinteger) tag attachedto: (ID<AccessViewTagProtocol>)ObjectSetupblock: (viewsetupblock) block {UIView*tmpview = [[Selfclass] alloc] initwithframe:frame];        [Tmpview Supportaccessviewtagprotocol]; View&& [View Iskindofclass:[uiviewclass]]                     ? ([View Addsubview:tmpview]):0; Object&& [ObjectRespondstoselector: @selector (setview:withtag:)]? ([Tmpview Settag:tag attachedto:Object]) :0; if(block) {block (Tmpview); }        returnTmpview;}+(Instancetype) Viewwithframe: (CGRect) frame Insertintoview: (UIView*) View Setupblock: (viewsetupblock) block {UIView*tmpview = [[Selfclass] alloc] initwithframe:frame];        [Tmpview Supportaccessviewtagprotocol]; View&& [View Iskindofclass:[uiviewclass]] ? ([View Addsubview:tmpview]):0; if(block) {block (Tmpview); }        returnTmpview;}+ (Instancetype) Lineviewinsertintoview: (UIView *) View Positiony: (cgfloat) positiony thick: (cgfloat) thick leftgap: (cgfloat) Leftgap rightga P: (cgfloat) Rightgap color: (Uicolor*) Color {UIView*tmpview = [[Selfclass] Alloc] Initwithframe:cgrectmake (Leftgap, Positiony, View.frame.size.width-leftgap-rightgap, thick)]; Color? Tmpview.backgroundcolor = color:0;        [View Addsubview:tmpview]; returnTmpview;}+ (Instancetype) Lineviewinsertintoview: (UIView *) View Positionx: (cgfloat) Positionx thick: (cgfloat) thick topgap: (cgfloat) Topgap Bottomga P: (cgfloat) Bottomgap color: (Uicolor*) Color {UIView*tmpview = [[Selfclass] Alloc] Initwithframe:cgrectmake (Positionx, topgap, thick, View.frame.size.height-topgap-Bottomgap)]; Color? Tmpview.backgroundcolor = color:0;        [View Addsubview:tmpview]; returnTmpview;}#pragmaMark-runtime property.-(void) Settagnumbervalue: (NSNumber *) Tagnumbervalue {objc_setassociatedobject (self, @selector (tagnumbervalue), Tagnumbervalue, Objc_association_re tain_nonatomic);}-(NSNumber *) Tagnumbervalue {returnObjc_getassociatedobject (self, _cmd);}- (void) Setviewsweakmap: (nsmaptable<nsstring *,uiview *> *) Viewsweakmap {objc_setassociatedobject (self, @selector (viewsweakmap), Viewsweakmap, Objc_association_retain_n onatomic);}-(nsmaptable<nsstring *,uiview *> *) Viewsweakmap {returnObjc_getassociatedobject (self, _cmd);}#pragmamark-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

Details

Need to implement protocol (use nsmaptable's strongtoweakobjectsmaptable as storage String-view)

Getting the tag is more convenient, not relying on which view to get the view from, but getting it directly from the nsmaptable

Convenient initialization of view and setting tag value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.