嘗試動畫蒙上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