jonathananesand
jonathananesand

Reputation: 86

Animate UISegmentedControl image

Im trying to animate an image in a UISegmentedControl. This is what I have come up with but it doesn't work.

The animation itself works fine if I switch imageView.layer to _segmentedControl.layer in the last line of code.

What am I missing?

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

Upvotes: 2

Views: 953

Answers (2)

jonathananesand
jonathananesand

Reputation: 86

For future reference I would like to share the solution I came up with. Instead of animating the image for the segment, which doesn't seem possible, you can at least animate the subview of the segmented control.

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

Upvotes: 2

shalunv
shalunv

Reputation: 54

You use the collection UIView. UISegmentedControl can not animate

Upvotes: 0

Related Questions