iOS iPhone 是否可以克隆 UIView 并将其自身绘制为两个 UIView? [英] iOS iPhone is it possible to clone UIView and have it draw itself to two UIViews?

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

问题描述

我正在考虑一种让 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?

推荐答案

不知道你的真正意图是什么,但这会绘制视图两次,用户交互等不适用于第二个视图.此外,此解决方案不会处理不同的帧大小.

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 并将其自身绘制为两个 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆