2016-03-06 154 views
1

我想根據材料設計,以實現兩個的UIView之間過渡動畫上截圖如下所示(僅用於動畫的第一部分):添加比例動畫CAShapeLayer

enter image description here

但我喜歡將動畫蒙版應用於UIView更加逼真。這裏是我開發的代碼:

- (IBAction)onButtonTapped:(UIButton *)sender 
{ 
    // 1. make a hole in self.view to make visible another view 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.view.bounds]; 
    [maskPath appendPath:[UIBezierPath bezierPathWithArcCenter:sender.center 
                 radius:sender.frame.size.width/2.0f 
                startAngle:0.0f 
                 endAngle:2.0f*M_PI 
                clockwise:NO]]; 

    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.fillRule = kCAFillRuleEvenOdd; 
    maskLayer.fillColor = [UIColor blackColor].CGColor; 
    maskLayer.path = maskPath.CGPath; 

    // 2. add scale animation for resizing hole 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    animation.fromValue = [NSValue valueWithCGRect:sender.frame]; 
    animation.toValue = [NSValue valueWithCGRect:self.view.bounds]; 
    animation.duration = 3.0f; 
    [maskLayer addAnimation:animation forKey:@"scaleAnimation"]; 

    self.view.layer.mask = maskLayer; 
} 

我的想法是在模態UIView中添加一個「洞」,然後縮放機智動畫。

問題是它無法正常工作。代碼的第一部分很好地打好了一個洞。但縮放一個洞的動畫完全不起作用。

回答

0

如果圖層不在某個窗口的圖層樹中,則無法將動畫添加到圖層。您需要按以下順序執行操作:

self.view.layer.mask = maskLayer; 
[CATransaction flush]; 
[maskLayer addAnimation:animation forKey:@"scaleAnimation"]; 
+0

仍然不能如上面截圖所示 – Sergio