Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将CAlayer分配给UIImageView,以便能够使用UIImageView执行所有操作_Iphone_Xcode_Animation_Uiimageview_Calayer - Fatal编程技术网

Iphone 将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 {

大家好,这里有一个链接:其中有一个示例代码,作者将CALayer与CAShapeLayer一起使用。我想使用这个CALayer,就像它是一个uiimageView(可能分配给uiimageView)一样,以便能够移动它等等。

您不能更改现有
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”?否。如果您要使用纯基于层的解决方案,则需要在该委托中的implement
drawLayer:inContext:
中为您的
CAShapeLayer
指定一个委托。搜索有关如何使用图层绘制图形的示例。我向您介绍的第二个解决方案是使用
UIView
。每个
UIView
都有一个支持层,您可以通过
layer
属性访问它的层。您可以告诉
UIView
使用
layerClass
的背衬层使用哪种层。