iphone - 屏蔽 UIView、CALayer 的动画

标签 iphone ios ipad animation calayer

我正在努力实现以下目标: 用户点击一个 View ,一个圆形 View 会弹出到它的左侧,其中包含一些图像内容。 View 应该从触摸点开始动画到触摸 View 之外的最后一帧并向左移动。在动画期间它应该是一个圆圈,增长到正确的位置和大小

下面的代码一切正常,只是在动画期间,圆形边界只在左边。就像CALayer形状正在滑入其最终框架。

看起来有点像这样。

enter image description here

动画完成后,我得到了预期的完整圆圈。

CGFloat w = 300;
CGRect frame = CGRectMake(myX, myY, w, w);
CGPoint p = [touch locationInView:self.imageView];
CGRect initialFrame = CGRectMake(p.x, p.y, 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,但这也不会导致令人满意的结果,因为半径必须随着帧大小而改变

最佳答案

您正在为框架而不是 mask 设置动画。圆形 mask 的大小保持不变,您的图像帧从左上角动画到最终的全尺寸。这就是为什么您对图像的左上部分进行圆形裁剪,直到达到最终帧大小为止。

这就是您的动画中基本发生的事情: enter image description here

您可以尝试对变换进行动画处理(这将完全按照您希望的样子变换最终剪裁的图像)而不是对帧进行动画处理。

类似于:

// 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);
}];

关于iphone - 屏蔽 UIView、CALayer 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564336/

相关文章:

ios - 如何使用可选的 URL 参数为 GET 请求映射不同的 JSON 响应

ios - 为模拟器构建时为 "Redefinition of module ..."

ios - iOS 编程中的可达性 AlertView

iphone - 在另一个类中更改 NSString 时更新 UILabel 文本

c# - 使用iphone从PC发送短信

ios - 如何在头文件 (.h) 中创建一个不可变的只读属性,在实现 (.m) 中创建一个可变的读写属性

iphone - 从继承类调用方法

ios - PushKit 不接受凭据

iphone - 当 iPad 日期时间设置更改时通知应用程序

ios - 管理您的应用安排的本地通知的好方法是什么?