本文介绍了iOS iPhone是否可以克隆UIView并让它自己绘制到两个UIViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑让UIView渲染到另一个UIView以及第一个UIView的方法。所以我的主要UIView有它的界限,UIView也在其他UIView中渲染。

I'm thinking of a way to have a UIView render itself onto another UIView as well as the first one. So I have my main UIView with it's bounds, and the UIView also renders itself in some other UIView.

这可能吗?它是否需要大量的图层操作?

Is this possible ? Does it require extensive layer operations?

推荐答案

不知道你的真实意图是什么,但这会画两次视图, userinteraction等将不适用于第二个视图。此解决方案也不会考虑不同的帧大小。

Don't know whats your real intention is, but this will draw the view twice, userinteraction etc. will not work on the second view. Also this solution does not take care of different frame sizes.

您要克隆的视图的标题

@interface SrcView : UIView
@property(nonatomic, readonly, strong) UIView *cloneView;
@end

@interface CloneView : UIView
@property(nonatomic, weak) UIView *srcView;
- (id)initWithView:(UIView *)src;
@end

实施您要克隆的视图

#import "SrcView.h"
#import "CloneView.h"

@implementation SrcView
@synthesize cloneView;

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    [cloneView setNeedsDisplay];
}

- (UIView *)cloneView {
    if (!cloneView) {
        cloneView = [[CloneView alloc] initWithView:self];
    }
    return cloneView;
}

@end

@implementation CloneView
@synthesize srcView;

- (id)initWithView:(UIView *)src {
    self = [super initWithFrame:src.frame];
    if (self) {
        srcView = src;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [srcView.layer renderInContext:UIGraphicsGetCurrentContext()];
}

@end

现在你可以调用cloneView并将它添加到你想要的地方。

now you can just call cloneView and add it somewhere you want.

这篇关于iOS iPhone是否可以克隆UIView并让它自己绘制到两个UIViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:34