蒙面UIView,CALayer的animation

我试图达到以下目的:用户点击一个视图,一个圆形视图popup到它的左侧,其中的一些图像内容。 该视图应该从触摸点开始到在触摸视图之外的最后一帧开始animation。 在animation中,它应该是一个圆圈,成长到正确的位置和大小

下面的代码都可以正常工作,只有在animation过程中 ,圆形边界只在左边。 这就像CALayer形状正在滑入其最终框架。

看起来有点像这样

在这里输入图像说明

一旦animation完成,我就有预期的整个圆。

 CGFloat w = 300; CGRect frame = CGRectMake(myX, myY, w, w); CGPoint p = [touch locationInView:self.imageView]; CGRect initialFrame = CGRectMake(px, py, 0,0); UIImageView *circle = [[UIImageView alloc] initWithFrame:frame]; circle.image = [UIImage imageNamed:@"china"]; circle.contentMode = UIViewContentModeScaleAspectFill; circle.backgroundColor = [UIColor clearColor]; circle.layer.borderWidth = 1; circle.layer.borderColor = [UIColor grayColor].CGColor; circle.layer.masksToBounds = YES; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; CGRect maskRect = CGRectMake(0, 0, w, w); CGMutablePathRef path = CGPathCreateMutable(); CGPathAddEllipseInRect(path, nil, maskRect); maskLayer.path = path; CGPathRelease(path); circle.layer.mask = maskLayer; circle.frame = initialFrame; [self.imageView addSubview:circle]; [UIView animateWithDuration:1.0 animations:^{ circle.frame = frame; }]; 

我已经尝试过与CALayercornerRadius一起工作,但是这也不能产生令人满意的结果,因为半径也必须随着框架尺寸而改变。

你是animation框架,但不是面具。 圆形的面具保持不变的大小,你的图像框从左上angularanimation到最后的完整大小。 这就是为什么你得到的图像左上angular的圆形剪辑,直到它到达压轴帧大小。

这是基本上发生在您的animation: 在这里输入图像说明

您可以尝试对变换进行animation处理(它将完全按照您想要的方式来变换压轴剪裁的图像),而不是animation化帧。

就像是:

 // Don't animate the frame. Give it the finale value before animation. circle.frame = frame; // Animate a transform instead. For example, a scaling transform. circle.transform = CGAffineTransformMakeScale(0, 0); [UIView animateWithDuration:1.0 animations:^{ circle.transform = CGAffineTransformMakeScale(1, 1); }]; 

你有没有尝试使用循环视图的autoresizingMask,让它有灵活的左/右边距?

这可能会在animation中扮演一个angular色,并解释为什么您的视图出现“滑动”。 (至less这是值得一试)