UIView duplicate
I need to duplicate a view, with all it's subviews.
Does anyone knows how to do i开发者_JAVA百科t?
The easiest (but probably not the fastest) way is to archive it into an NSData object and unarchive it right away.
NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:myView];
UIView *myViewDuplicate = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
To archive any uiimage object simply convert them to an imagerep of one kind or another (PNG for instance) and archive that NSData object, then in the unarchive you restore the uiimage with imageWithData:
Here is a new method you can use: Use UIView's method:
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
This is the fastest way to draw a view. Available in iOS 7.
One can easily copy a UIView by archiving and unarchiving process.
NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:myView];
UIView *myViewDuplicate = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
But this ignores UIImageView, so for UIImageView do it by this way,
UIImage *img = [UIImage imageNamed: @"myImage"];
UIImage *img2 = [UIImage imageWithCGImage:img.CGImage scale:img.scale orientation:img.imageOrientation];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:img2];
UIImage *imgCopy = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%@, %@", imgCopy, NSStringFromCGSize(imgCopy.size)); // -> <UIImage: 0x7fa013e766f0>, {50, 53}
Refrence from here.
Swift version
This answer shows how to do the same thing in Swift as the accepted answer does in Objective-C.
let tempArchive = NSKeyedArchiver.archivedDataWithRootObject(myView)
let myViewDuplicate = NSKeyedUnarchiver.unarchiveObjectWithData(tempArchive) as! UIView
My full answer with more notes is here.
You can drag your custom view outside the root view of the viewController. In the implementation of the custom view class, override the enconde/decode methods. Realize the IBOutlets and IBActions connections will not be archived in memory.
@implementation MyCustomView
-(MyCustomView*)aCopy{
NSData *temp = [NSKeyedArchiver archivedDataWithRootObject:self];
return [NSKeyedUnarchiver unarchiveObjectWithData:temp];
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:self.btnBack forKey:@"btnBack"];
[coder encodeObject:self.lbl1 forKey:@"lbl1"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
self.btnBack = [aDecoder decodeObjectForKey:@"btnBack"];
self.lbl1 = [aDecoder decodeObjectForKey:@"lbl1"];
[_btnBack addTarget:self action:@selector(onBtnBack) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (IBAction)onBtnBack{
//. . .
MyCustomView.h:
#import <UIKit/UIKit.h>
@interface MyCustomView : UIView
@property (weak, nonatomic) IBOutlet UILabel *lbl1;
@property (weak, nonatomic) IBOutlet UIButton *btnBack;
-(MyCustomView*)aCopy;
@end
Sample of reusing the custom view in ViewController.m:
#import "ViewController.h"
#import "MyCustomView.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet MyCustomView *myCustomView;
@property (weak, nonatomic) IBOutlet UIView *vwPlaceHolder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:_myCustomView];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MyCustomView *vwCopy = [_myCustomView aCopy];
vwCopy.lbl1.text = @"My other msg";
vwCopy.frame = _vwPlaceHolder.frame;
[_vwPlaceHolder removeFromSuperview];
[self.view addSubview:vwCopy];
}
@end
精彩评论