Iphone 将CAlayer分配给UIImageView,以便能够使用UIImageView执行所有操作
大家好,这里有一个链接:其中有一个示例代码,作者将CALayer与CAShapeLayer一起使用。我想使用这个CALayer,就像它是一个uiimageView(可能分配给uiimageView)一样,以便能够移动它等等。您不能更改现有Iphone 将CAlayer分配给UIImageView,以便能够使用UIImageView执行所有操作,iphone,xcode,animation,uiimageview,calayer,Iphone,Xcode,Animation,Uiimageview,Calayer,大家好,这里有一个链接:其中有一个示例代码,作者将CALayer与CAShapeLayer一起使用。我想使用这个CALayer,就像它是一个uiimageView(可能分配给uiimageView)一样,以便能够移动它等等。您不能更改现有UIView(意思是:您也不能更改UIImageView的图层),但是绘制图层非常简单:您需要为CALayer指定一个代理,该代理需要实现 另一个解决方案可能是创建UIView子类并实现+layerClass: + (Class)layerClass {
UIView
(意思是:您也不能更改UIImageView
的图层),但是绘制图层非常简单:您需要为CALayer
指定一个代理,该代理需要实现
另一个解决方案可能是创建UIView
子类并实现+layerClass
:
+ (Class)layerClass
{
return [CAShapeLayer class];
}
这样,您的视图将使用一个形状层,您可以简单地使用通常的UIView
的drawRect:
方法。然后您可以通过(CAShapeLayer*)[self layer]
访问该层来修改它。不过,我还没有尝试过这种方法
编辑:解释第二个解决方案将如何完成:
@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来移动它,就像:imageView.center=CGPointMake(imageView.center.x+x,imageView.center.y+y);而不是imageView放我的图层:)我真的不明白:+(Class)layerClass{return[CAShapeLayer Class];}它告诉您的
UIView
使用CAShapeLayer
作为后备存储。请参阅。在示例代码中,CALayer的名称是“rootLayer”,因此我必须将其替换为“layerClass”“rootLayer”?否。如果您要使用纯基于层的解决方案,则需要在该委托中的implementdrawLayer:inContext:
中为您的CAShapeLayer
指定一个委托。搜索有关如何使用图层绘制图形的示例。我向您介绍的第二个解决方案是使用UIView
。每个UIView
都有一个支持层,您可以通过layer
属性访问它的层。您可以告诉UIView
使用layerClass
的背衬层使用哪种层。