Cercando di animare un'ellisse mascherata su un UIView per trasformarla in scala rimanendo nella posizione centrale.CABasicAnimazione - Trasforma la scala in centro
ho trovato CALayer - CABasicAnimation not scaling around center/anchorPoint, e seguita da aggiunta di una proprietà bounds
al maskLayer
CAShapeLayer
tuttavia, questo termina con la maschera essendo posizionato nell'angolo sinistro con soltanto 1/4 di esso mostrando. Vorrei che la maschera rimanesse nel centro dello schermo.
@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];
}
Animation ...
-(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"];
}
Aggiornamento
Mi sembra di aver risolto questo con un secondo di animazione sul tasto position
. È corretto o esiste un modo migliore per farlo?
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"];
Il ridimensionamento non influisce sulla conversione eseguita dopo? Lo sto testando (così come con la normale trasformazione affine) e la vista finisce sempre in alto a sinistra, lontano dalla sua posizione iniziale. – Ixx
Questo ha funzionato nel mio caso 'CABasicAnimation * scale = [CABasicAnimation animationWithKeyPath: @" transform "]; CATransform3D tr = CATransform3DIdentity; tr = CATransform3DScale (tr, 3, 3, 1); scale.toValue = [valore NSValoreWithCATransform3D: tr]; ' –