尝试动画蒙上UIView的椭圆,将比例变换保留在中心位置。CABasicAnimation - 变换比例保持居中
我发现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"];
缩放比例是否会影响您在之后做的翻译?我正在测试这个(以及正常的仿射变换),并且视图始终在左上方离开它的初始位置。 – Ixx 2016-07-02 16:47:58
这个在我的案例中工作 'CABasicAnimation * scale = [CABasicAnimation animationWithKeyPath:@“transform”]; CATransform3D tr = CATransform3DIdentity; tr = CATransform3DScale(tr,3,3,1); scale.toValue = [NSValue valueWithCATransform3D:tr];' – 2017-03-28 06:58:22