2013-05-08 17 views
14

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 maskLayerCAShapeLayer 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"]; 

risposta

6

Mi sembra di aver risolto questo problema con una seconda animazione sul tasto di posizione. È 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"]; 
12

È possibile creare una scala attorno al centro dell'oggetto, invece di (0, 0) in questo modo:

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]; 

Così abbiamo iniziare con l ' "identità" trasformare (che significa 'no trasformare'). Quindi traduciamo (spostati) al centro dell'oggetto. Quindi scaliamo. Poi torniamo indietro a origo, quindi siamo tornati dove abbiamo iniziato (volevamo solo influenzare l'operazione di scala).

+0

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

+0

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]; ' –

4

Non so perché, ma CAShapeLayer non imposta la proprietà bounds sul layer. Nel mio caso, questo era il problema. Prova ad impostare i limiti. Questo dovrebbe funzionare

ho fatto qualcosa di simile per la costruzione del cerchio iniziale

self.circle = [CAShapeLayer layer]; 
CGRect roundedRect = CGRectMake(0, 0, width, width); 
self.circle.bounds = roundedRect; 
UIBezierPath *start = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:width/2]; 
[self.circle setPath:start.CGPath]; 
self.circle.position = CGPointMake(whatever suits you); 
self.circleView.layer.mask = self.circle; 
[self.circleView.layer.mask setValue: @(1) forKeyPath: @"transform.scale"]; 

E qualcosa di simile per ridimensionarla

CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
scale.fromValue = [self.circleView.layer.mask valueForKeyPath:@"transform.scale"]; 
scale.toValue = @(100); 
scale.duration = 1.0; 
scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
[self.circleView.layer.mask setValue:scale.toValue forKeyPath:scale.keyPath]; 
[self.circleView.layer.mask addAnimation:scale forKey:scale.keyPath]; 

PD: AnchorPoint è di default (0,5 ,. 5) secondo i documenti Apple ... ma sappiamo tutti che non significa niente giusto?

Spero che aiuti !!

3

Ho scritto la seguente funzione con molte modifiche e opzioni aggiuntive, potrebbe essere utile per molti altri.

func layerScaleAnimation(layer: CALayer, duration: CFTimeInterval, fromValue: CGFloat, toValue: CGFloat) { 
    let timing = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn) 
    let scaleAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale") 

    CATransaction.begin() 
    CATransaction.setAnimationTimingFunction(timing) 
    scaleAnimation.duration = duration 
    scaleAnimation.fromValue = fromValue 
    scaleAnimation.toValue = toValue 
    layer.add(scaleAnimation, forKey: "scale") 
    CATransaction.commit() 
} 

Nota: Non dimenticare di aggiornare dimensioni dopo il completamento di animazione, perché CATransaction torna a dimensioni reali.

+0

scrubber? ,.,./,. –

+0

si suppone che lo scrubber sia uno strato? anywas. mi ha aiutato molto bene grazie>) –

+1

@DavidSeek si il suo layer non è scrubber, ho aggiornato la mia risposta grazie. – Aamir