2014-03-14 94 views
20

我想創建一個動畫,它將通過一個因子來調整UIView及其內容的大小。基本上,我想製作一個動畫,首先展開視圖,然後將其縮回到原始大小。製作動畫以展開和縮小UIView

這樣做的最好方法是什麼?我嘗試了CALayer.contentScale,但它什麼也沒做。

+0

相關http://stackoverflow.com/questions/5446899/scale-zoom-into-a-uiview-at-a-point或http://stackoverflow.com/questions/18329286/how-to-scale -zoom-A-的UIView到一個給出的-cgpoint?LQ = 1? – tophyr

回答

44

可以嵌套一些動畫塊一起,像這樣:

目的-C:

[UIView animateWithDuration:1 
       animations:^{ 
        yourView.transform = CGAffineTransformMakeScale(1.5, 1.5); 
       } 
       completion:^(BOOL finished) { 
        [UIView animateWithDuration:1 
             animations:^{ 
              yourView.transform = CGAffineTransformIdentity; 

             }]; 
       }]; 

夫特2.0:

UIView.animateWithDuration(1, animations: {() -> Void in 
    yourView.transform = CGAffineTransformMakeScale(1.5, 1.5) 
    }) { (finished: Bool) -> Void in 
     UIView.animateWithDuration(1, animations: {() -> Void in 
      yourView.transform = CGAffineTransformIdentity 
     })} 

夫特3.0:

UIView.animate(withDuration: 1, animations: { 
    yourView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) 
}) { (finished) in 
    UIView.animate(withDuration: 1, animations: { 
     yourView.transform = CGAffineTransform.identity 
    }) 
} 

和替換s使用你自己的價值和持續時間。

8

這裏是一個小辦法,也循環:

[UIView animateWithDuration:1 
         delay:0 
        options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat 
       animations:^{ 
        yourView.transform = CGAffineTransformMakeScale(1.5, 1.5); 
       } 
       completion:nil]; 

選項UIViewKeyframeAnimationOptionRepeat是什麼使得它循環,如果你不希望它保持「呼吸」。動畫塊作爲「吸氣」,UIViewKeyframeAnimationOptionAutoreverse選項自動播放「呼氣」動畫。