ios - CABasicAnimation - 变换比例保持在中心

标签 ios core-animation core-graphics mask catransform3d

尝试对 UIView 上的椭圆进行动画处理,使其保持在中心位置进行缩放转换。

我找到了 CALayer - CABasicAnimation not scaling around center/anchorPoint ,然后将 bounds 属性添加到 maskLayer CAShapeLayer 然而,这以掩码位于左角只有 1/其中有 4 个显示。我希望蒙版保持在屏幕中央。

@synthesize maskedView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"];

    vc.view.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
vc.view.layer.bounds = self.view.layer.bounds;

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    CGRect maskRect = CGRectMake((self.view.frame.size.width/2)-50, (self.view.frame.size.height/2)-50, 100, 100);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, nil, maskRect);
    [maskLayer setPath:path];

    CGPathRelease(path);

    vc.view.layer.mask = maskLayer;
    [self.view addSubview:vc.view];

    maskedView = vc.view;
    [self startAnimation];
}

动画...

-(void)startAnimation
{
    maskedView.layer.mask.bounds = CGRectMake((self.view.frame.size.width/2)-50, (self.view.frame.size.height/2)-50, 100, 100);
    maskedView.layer.mask.anchorPoint = CGPointMake(.5,.5);
    maskedView.layer.mask.contentsGravity = @"center";

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animation.duration = 1.0f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:5.0f];

    [maskedView.layer.mask addAnimation:animation forKey:@"animateMask"];
}

更新

我似乎已经通过 position 键上的第二个动画修复了这个问题。这是正确的还是有更好的方法?

CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"position"];

animation2.duration = 1.0f;

animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation2.fromValue = [NSValue valueWithCGPoint:CGPointMake((self.view.frame.size.width/2), (self.view.frame.size.height/2))];
animation2.toValue = [NSValue valueWithCGPoint:CGPointMake((self.view.frame.size.width/2), (self.view.frame.size.height/2))];

[toBeMask.layer.mask addAnimation:animation2 forKey:@"animateMask2"];

最佳答案

您可以围绕对象的中心而不是 (0, 0) 创建一个比例尺,如下所示:

CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D tr = CATransform3DIdentity;
tr = CATransform3DTranslate(tr, self.bounds.size.width/2, self.bounds.size.height/2, 0);
tr = CATransform3DScale(tr, 3, 3, 1);
tr = CATransform3DTranslate(tr, -self.bounds.size.width/2, -self.bounds.size.height/2, 0);
scale.toValue = [NSValue valueWithCATransform3D:tr];

所以我们从“身份”转换开始(意思是“无转换”)。然后我们平移(移动)到物体的中心。然后我们扩展。然后我们回到原点,这样我们就回到了开始的地方(我们只想影响缩放操作)。

关于ios - CABasicAnimation - 变换比例保持在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16447236/

相关文章:

objective-c - CIFilter - 无法更改名称

ios - 将 UILabel 位置更改为 CGPath 的 strokeEnd

ios - 如何设置 CGBITMAP_CONTEXT_LOG_ERRORS 环境变量?

UIImage的ressizedImageWithCapInsets在iOS 7下带来多个CoreGraphics "invalid context 0x0"错误

iphone - 两个lat long ios之间距离的准确性

ios - 修改单元格 subview 的框架

ios - 如何通过代码获取 iOS 设备的 GPU 信息

ios - 在将大型 UIImage 绘制到其中后,CGContextDrawImage 非常慢

ios - 向上/向下滑动手势在 UIWebView 中不起作用

iOS 和 Swift - 获取一个对象的屏幕位置以设置另一个对象的位置