问题描述
喜每个人有一个链接:,有一个样品code在它在这里笔者使用的CALayer用CAShapeLayer.What我想就是用这个的CALayer就像是一个UIImageView(可能分配给一个UIImageView)能够进行移动等...
Hi every one there is a link :link and there is a sample code in it where the author use a CALayer with CAShapeLayer.What I would like is to use this CALayer like it was a uiimageView (maybe assign to a uiimageview) to be able to move it etc...
推荐答案
您不能改变的图层类型现有的的UIView
(意思是:你也改变不了的层的的UIImageView
),但画上一个层是pretty简单:你需要一个委托分配给您的的CALayer
,并且该委托需要实现 drawLayer:inContext的:
You cannot change the layer type of an existing UIView
(meaning: you also can't change the layer of an UIImageView
), but drawing to a layer is pretty easy: you need to assign a delegate to your CALayer
, and that delegate needs to implement drawLayer:inContext:.
另一种解决方案可能是创建一个的UIView
子类,并实施 + layerClass
:
Another solution might be to create a UIView
subclass and implement +layerClass
:
+ (Class)layerClass
{
return [CAShapeLayer class];
}
这样,你的观点将使用形状图层,你也许可以简单地使用通常的的UIView
的drawRect:
方法。然后,您可以通过访问层(CAShapeLayer *)[个体经营层]
进行修改。我没有试过这种方法,虽然。
That way your view will use a shape layer and you might be able to simply use the usual UIView
's drawRect:
method. You could then access the layer via (CAShapeLayer *)[self layer]
to modify it. I haven't tried this approach, though.
编辑:如何解释第二的解决办法来完成:
Explaining how the second solution would be done:
@interface MyView : UIView {
UIImage *image;
}
@property(retain) UIImage *image;
@end
@implementation MyView
@synthesize image;
+ (Class)layerClass
{
return [CAShapeLayer class];
}
- (void)dealloc
{
self.image = nil;
[super dealloc];
}
- (void)drawRect:(CGRect)rect
{
// Draw the image. You might need to play around with this,
// for example to draw the image as aspect fit or aspect fill.
[image drawInRect:rect];
}
@end
这篇关于一个CALayer的分配给一个UIImageView才能够做所有你可以用一个UIImageView做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!