ios - 对 UISegmentedControl 图像进行动画处理

标签 ios objective-c animation uisegmentedcontrol

我正在尝试在 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"];

最佳答案

为了供将来引用,我想分享我想出的解决方案。您至少可以为分段控件的 subview 设置动画,而不是为分段的图像设置动画(这似乎不可能)。

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

关于ios - 对 UISegmentedControl 图像进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19751960/

相关文章:

ios - 从 nib 自定义 UiTableViewCell 而不重用?

c# - Unity3D - 是否可以通过脚本设置动画剪辑的属性?

ios - 使用 CAAnimation.CurrentMediaTime() 更改显式动画的速度

oop - 动画与MVC原理

ios - Xcode 9.2 Storyboard无法正确显示

iphone - Facebook 授权应用程序中的对话而不是 Safari 中的对话

ios - iOS后台执行有什么限制

iOS - 以编程方式切换 WiFi 网络 - 越狱

ios - 搜索 Youtube API 时描述被截断

objective-c - NSOperationQueue 出现非常奇怪的错误