UIView的图层蒙版animation设置框架

我想创build一个UIView子类掩盖自己,以便我添加到它的任何子视图是由一个圆圈裁剪。 我想要这个视图,它是在IB中定义的子视图,以便我可以轻松地为孩子定义布局约束。 到目前为止,我有以下…

@interface BubbleView () // eg: this is an example of a child view that would be "under" a mask @property(weak,nonatomic) IBOutlet UIImageView *imageView; @end @implementation BubbleView // not really sure if this kind of init is the right pattern, but it seems to work and // I don't think this is my current problem?? + (instancetype)bubbleViewFromNib { BubbleView *view = [[NSBundle mainBundle] loadNibNamed:@"BubbleView" owner:nil options:nil][0]; UIImage *_maskingImage = [UIImage imageNamed:@"circlemask"]; CALayer *maskingLayer = [CALayer layer]; [view.layer addSublayer:maskingLayer]; maskingLayer.contents = (__bridge id _Nullable)(_maskingImage.CGImage); view.layer.mask = maskingLayer; return view; } - (void)layoutSubviews { self.layer.mask.frame = self.bounds; } 

(注意:我在IB中给出了一个紫色,所以我可以看到发生了什么)

这几乎工作,但是当拥有视图控制器调整这个视图,像这样…

 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { BubbleView *b = (BubbleView *)[self.view viewWithTag:222]; //b.transform = CGAffineTransformScale(b.transform, 1.2, 1.2); b.frame = CGRectInset(b.frame, -30,-30); } 

面具做了一些奇怪的事情:它保持小“跳”到视图的左上angular,然后非常快地animation到正确的大小(大30:30)。 为什么它会animation?

之前…

在这里输入图像说明

像这样的快速animation…

在这里输入图像说明

将NSLog放置在layoutSubviews中,我注意到它被调用两次,这很奇怪,但仍然没有足够的时间来解释快速animation。

另外,当我改变变换而不是框架,它完全调整,没有animation。 但是我需要做框架和变换的变化。

有人能告诉我我哪里出了错?

设置图层的animation属性时,除非该图层是UIView的主图层,否则隐式animation是默认的。 此外, frame属性仅仅是boundsposition属性的正面。 出于这个原因,你不应该直接设置图层的frame ; 总是设定自己的boundsposition 。 如果您不需要animation,则需要closures这些属性的隐式animation; 最简单的方法是完全closures当前CATransaction( setDisableActionstrue ),但是有更微妙和精确的方法来完成同样的事情。