Howto CALayer 的图像比例独立于 CALayer 的边界
我找不到这个问题的答案。
我想知道如何使 calayer 中的图像大小低于 calayer 的边界大小。
我在 iPad 游戏中有几个 pawn,每个都是 CALayer,我只需使用 contentGravity=kCAGravityResizeAspect 即可调整它们的大小。图像在 30x30 的 CALayer 内为 128x128,因此图像会自动调整为 30x30,并且由于两者都是盒子,因此宽高比保持不变并有效。
在这里,我将 CALayer 的边界设置为与超级视图的大小成比例,因此 Pawn 始终向视图呈现相同的相对大小。这个位于我的 sprite 类子类 calayer 中:
-(void) setSpriteScaleToDice {
CGFloat newSize = [self superlayer].bounds.size.width * 0.066666667f;
self.bounds=CGRectMake(0.0f, 0.0f, newSize, newSize);
self.contentsGravity = kCAGravityResizeAspect;
}
请注意,在我的例子中,CALayer 边界的最大尺寸为 30x30,这对于触摸来说很小。这就是我面临的问题,由于尺寸太小,很难“触摸”它们,有时触摸会失败......
我想到的想法之一是增加 Calayer 的“边界”,同时保持图像以其原始大小。问题是我已经搜索了很多并尝试了 contentGravity、contentsCenter、contentsScale 等多个选项...但没有成功。
特别是,根据苹果文档,似乎要走的路是使用contentsCenter(而不是使用contentsGravity),但是我的位图变形了......
拜托,任何想法都非常受欢迎,提前感谢,
Luis
I can't find an answer for this one.
I would like to know how to have the image size in a calayer's to be lower than calayer's bound's size.
I've got several pawns in an iPad game, each is a CALayer and I have them resize simply with a contentsGravity=kCAGravityResizeAspect. Image is 128x128 inside of a CALayer of 30x30 so the image gets resized automatically to 30x30 and because of both being a box, aspect ratio maintains and works.
Here I set CALayer's bounds proportional relative to superview's size, so the Pawns always present the same relative size to the view. This one is inside my sprite class subclass of calayer:
-(void) setSpriteScaleToDice {
CGFloat newSize = [self superlayer].bounds.size.width * 0.066666667f;
self.bounds=CGRectMake(0.0f, 0.0f, newSize, newSize);
self.contentsGravity = kCAGravityResizeAspect;
}
Note that in my case the CALayer bounds gets a maximum of 30x30 which is small for a touch. That's the problem I'm facing, due to this small size it's difficult to "touch" them, sometimes touch fails...
One of the ideas that I'm thinking is to increase the "bounds" of the calayer, while keeping the image at its original size. The problem is that I've search a lot and tried several options with contentsGravity, contentsCenter, contentsScale, etc... without success.
In particular, as per apple docs looks like the way to go is with contentsCenter (and not using contentsGravity), however I get deformation in the bitmap...
Please, any idea is really welcome, and thanks in advance,
Luis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是一个愚蠢的问题,但为什么要使用 CALayers 而不是 UIViews 呢?
UIImageView
有一个contentMode
属性,可以让您轻松地做到这一点(更不用说更容易用于触摸事件处理)。也就是说,
CALayer
有一个contentsRect
属性,它似乎可以让您为要在其中绘制的内容定义一个子矩形,这样您就可以做您想做的事情。另一种选择是将图像层放置在更大的层中,并将其用于命中测试。
This is probably a silly question, but why are you using
CALayers
for this instead ofUIViews
?UIImageView
has acontentMode
property that lets you do this easily (not to mention being easier to use for touch event handling).That said,
CALayer
has acontentsRect
property that appears to let you define a sub-rectangle for contents to be drawn within, so that may let you do what you want.Another option would be to place your image layer inside a larger layer and use that for the hit test.
CAlayer CGFloat contentsScale
CAlayer CGFloat contentsScale
如果您希望在 CALayer 中以 CALayer 以外的尺寸绘制图像,则需要创建自己的
drawInContext:
方法并绘制图像,而不是设置 CALayer 的contents
属性。不要设置contents
属性,创建您自己的属性来跟踪您要绘制的图像。If you want your image drawn in the CALayer at a size other than the CALayer you need to create your own
drawInContext:
method and draw the image rather than setting the CALayer'scontents
property. Do not set thecontents
property, create your own to track the image you want to draw.