IOS copies a UIView object implementation method, and iosuiview object implementation
IOS copies a UIView object. A new user guide is required in the project, that is, a mask is added to a page, and a view is highlighted.
My idea is to add an identical view to the mask, but the problem is that a simple view can be done in this way. Is it necessary to create a more complex view, that's not too much trouble.
Can I copy it directly and add it to the mask?
We all know that copy and mutableCopy are used to copy arrays, dictionaries, and strings. Then I used my custom View to call copy. The result was very embarrassing and collapsed, the copyWithZone method is implemented.
There is always no way to implement all custom views!
I was so excited that the netizens gave me this method. Please give it a try.
NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:view]; UIView *copyView = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
However, this method is not perfect. Although all the child controls can be displayed in the copyView, the address is nii. Of course, this is not the key to the problem, the most fatal thing is that if the sub-control contains rounded corners, after the sub-control is copied, all the rounded corners will be ready. This cannot be tolerated, so I had this blog and thought of a stupid way to add the existing rounded corner to the copied View sub-control!
The Code is as follows:
@ Implementation GuideView-(instancetype) init {self = [super init]; if (self) {self. backgroundColor = [UIColor colorWithWhite: 0 alpha: 0.3]; self. frame = [UIScreen mainScreen]. bounds; self. alpha = 0;} return self;} + (instancetype) guideView {return [[GuideView alloc] init];}-(void) addToView :( UIView *) view {[self addToView: view containCorner: NO];}/** add to a View */-(void) addToView :( UIView *) view cont AinCorner :( BOOL) containCorner {// After copying, if a sub-control sets a rounded corner, the rounded corner becomes invalid after copying, and all sub-controls can be correctly displayed, but the addresses are all nil NSData * archiveData = [NSKeyedArchiver preview: view]; UIView * copyView = [NSKeyedUnarchiver unarchiveObjectWithData: archiveData]; // if the subcontrol contains rounded corner if (containCorner = YES) {// Replace the subcontrol NSData * subData = [NSKeyedArchiver archivedDataWithRootObject: view. subviews]; NSArray * subViews = [NSKeyedUnarchiv Er unarchiveObjectWithData: subData]; [copyView removeAllSubviews]; for (UIView * subview in subViews) {[copyView addSubview: subview];} // find the subcontrol with rounded corners in the original View for (UIView * subView in view. subviews) {if (subView. layer. cornerRadius! = 0) {// traverse the child control of the copied View and find the child control corresponding to it for (UIView * copySubView in copyView. subviews) {// set the rounded corner for the found control. Here, frame is used to determine whether it is the same control if ([NSStringFromCGRect (copySubView. frame) isw.tostring: NSStringFromCGRect (subView. frame)]) {copySubView. layer. cornerRadius = subView. layer. cornerRadius; copySubView. layer. masksToBounds = YES; break ;}}} copyView. frame = [view convertRect: view. bounds toView: kAppDelegate. window]; [self addSubview: copyView];}-(void) show {[kAppDelegate. window addSubview: self]; [UIView animateWithDuration: 0.3 animations: ^ {self. alpha = 1 ;}] ;}- (void) disMiss {[UIView animateWithDuration: 0.3 animations: ^ {self. alpha = 1;} completion: ^ (BOOL finished) {[self removeFromSuperview];}] ;}//-(void) touchesBegan :( NSSet
*) Touches withEvent :( UIEvent *) event {// [super touchesBegan: touches withEvent: event]; // [self disMiss]; //} @ end
: