如何动态改变CALayer的内容

发布于 2024-10-03 18:07:35 字数 949 浏览 3 评论 0原文

我正在创建一些 CALayers 并使用 CATransform3D 转换它们,

imageLayer.frame = CGRectMake(-22.0f, 188.0f, 180.0f, 50.0f);
imageLayer.transform = CATransform3DMakeRotation(120.0f * M_PI / 180.0f,
                                                 0.0f, 15.0f, 0.0f);
imageLayer.contents = (id)[[UIImage imageNamed:@"photo.png"] CGImage];
[caLayer addSublayer:imageLayer];

这里是一个带有输出的示例图像... alt text

我将未转换的图像保留为图像视图,以便我可以处理滑动。我有 10 张图像的数组。因此,当用户在主图像上滑动时,相应的图像应该加载到图层和图像视图中。我可以加载图像视图,但动态加载 CALayers 时遇到一些问题。

我已经尝试过

NSArray *array=[caLayer sublayers];
    caLayer = [array objectAtIndex:entryImageIndex];
    //caLayer.contents=(id)[[UIImage imageNamed:@"flashImage1.jpg"] CGImage];
    caLayer.contents=(id)[[UIImage imageWithCGImage:[imageArray objectAtIndex:entryImageIndex-1]]];

但这给了我错误。谁能告诉我如何用图像动态填充 CALayers...任何示例代码都会有很大帮助...

I am creating some CALayers and transforming them using CATransform3D using

imageLayer.frame = CGRectMake(-22.0f, 188.0f, 180.0f, 50.0f);
imageLayer.transform = CATransform3DMakeRotation(120.0f * M_PI / 180.0f,
                                                 0.0f, 15.0f, 0.0f);
imageLayer.contents = (id)[[UIImage imageNamed:@"photo.png"] CGImage];
[caLayer addSublayer:imageLayer];

here is an sample image with the output...
alt text

I have kept the untransformed image as image view so that i can handle swipes. I have an array of 10 images. So, when a user swipes on the main images the corresponding images should load up in the layers and image view. I can load the image view but had some problems loading the CALayers dynamically.

I have tried

NSArray *array=[caLayer sublayers];
    caLayer = [array objectAtIndex:entryImageIndex];
    //caLayer.contents=(id)[[UIImage imageNamed:@"flashImage1.jpg"] CGImage];
    caLayer.contents=(id)[[UIImage imageWithCGImage:[imageArray objectAtIndex:entryImageIndex-1]]];

But this gave me errors. Can anyone plz tell me how to populate the CALayers dynamically with images...Any sample code will be of great help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-10-10 18:07:35

而不是

    caLayer.contents=(id)[[UIImage imageWithCGImage:
[imageArray objectAtIndex:entryImageIndex-1]
]
];

尝试

    caLayer.contents=(id)[UIImage imageWithCGImage:
[imageArray objectAtIndex:entryImageIndex-1]
].CGImage;

caLayer.contents=(id)[imageArray objectAtIndex:entryImageIndex-1];

instead of

    caLayer.contents=(id)[[UIImage imageWithCGImage:
[imageArray objectAtIndex:entryImageIndex-1]
]
];

try

    caLayer.contents=(id)[UIImage imageWithCGImage:
[imageArray objectAtIndex:entryImageIndex-1]
].CGImage;

or

caLayer.contents=(id)[imageArray objectAtIndex:entryImageIndex-1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文