【问题标题】:Need assistance setting mask layer anchor point需要协助设置遮罩层锚点
【发布时间】:2015-08-12 12:17:23
【问题描述】:
CALayer *mask = [CALayer layer];
mask.contents = (id)[[self getImg]CGImage];
mask.frame = CGRectMake(0, 0, self.vview.frame.size.width, 100);
mask.anchorPoint = CGPointMake(0.5, 1.0);
self.vview.layer.mask = mask;

我正在尝试在我的UIView vview 上设置蒙版,vview 的高度为 300,但是当我设置 anchor point 时,蒙版层跳了大约 50 像素,我应该怎么做才能保持其原始位置。

没有任何遮罩的绿色视图

带遮罩但没有锚点的绿色 vview

带有遮罩和锚点的绿色 vview

【问题讨论】:

  • 可以添加屏幕截图吗?
  • @Or.Ron 请查看截图。

标签: ios objective-c calayer mask


【解决方案1】:

关于锚点的文档:

anchorPoint
 Property
Defines the anchor point of the layer's bounds rectangle. Animatable.

Declaration
SWIFT
var anchorPoint: CGPoint
OBJECTIVE-C
@property CGPoint anchorPoint
Discussion
You specify the value for this property using the unit coordinate space. The default value of this property is (0.5, 0.5), which represents the center of the layer’s bounds rectangle. All geometric manipulations to the view occur about the specified point. For example, applying a rotation transform to a layer with the default anchor point causes the layer to rotate around its center. Changing the anchor point to a different location would cause the layer to rotate around that new point.

For more information about the relationship between the frame, bounds, anchorPoint and position properties, see Core Animation Programming Guide.

几何操作:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/CoreAnimationBasics/CoreAnimationBasics.html

基于此,如果您不想移动蒙版,只需将锚点设置为

mask.anchorPoint = CGPointMake(0.5,0.5);

这是默认值。

【讨论】:

  • 是的,我需要设置锚点,因为我还为蒙版设置动画,如果我不更改从中心执行动画的锚点,我想从底部开始设置动画,这就是为什么我改变了锚点。
  • 你能检查一下 position 属性的值吗?这可能与您的预期不同。在您设置不同的锚点后,您的框架也会发生变化。考虑到这一点,您可能需要重新计算框架/边界/位置。
  • 是的,这正是我所要求的 :)
猜你喜欢
  • 2017-08-30
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
相关资源
最近更新 更多