2013-11-03 40 views
2

我試圖在UISegmentedControl中爲圖像設置動畫效果。這是我想出來的,但它不起作用。Animate UISegmentedControl圖像

如果在最後一行代碼中將imageView.layer切換到_segmentedControl.layer,動畫本身可以正常工作。

我錯過了什麼?

 UIImageView *imageView = [[UIImageView alloc] init]; 
     imageView.image = [_segmentedControl imageForSegmentAtIndex:0]; 
     CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
     scale.duration = 0.25; 
     scale.repeatCount = 2; 
     scale.autoreverses = YES; 
     scale.fromValue = [NSNumber numberWithFloat:1.0]; 
     scale.toValue = [NSNumber numberWithFloat:0.0]; 
     [imageView.layer addAnimation:scale forKey:@"scale"]; 

回答

2

爲了將來的參考,我想分享我提出的解決方案。您可以至少爲分段控件的子視圖設置動畫效果,而不是爲片段設置動畫效果。

 UIView *subView = [[_segmentedControl subviews] objectAtIndex:0]; 
     CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
     scale.duration = 0.25; 
     scale.repeatCount = 1; 
     scale.autoreverses = YES; 
     scale.fromValue = [NSNumber numberWithFloat:1.0]; 
     scale.toValue = [NSNumber numberWithFloat:0.95]; 
     [subView.layer addAnimation:scale forKey:@"scale"]; 
0

您使用集合UIView。 UISegmentedControl無法生成動畫

相關問題