I'm trying to hide or show a view based on the value of a DCRoundSwitch object, but UIView animation blocks are not animating - the changes are instantly applied. I have also tried animating the view's layer with CATransaction, but those changes were instantly applied as well. If the same animation block is moved to another location (say, viewWillAppear:), animation works fine.
Any help determining why animations are not working is greatly appreciated. Thanks!
Here's the animation code:
- (void)switchValueChanged:(id)sender
{
[UIView animateWithDuration:0.33
animations:^{
self.containerView.alpha = self.switchView.isOn ? 1 : 0;
}];
}
For reference, here's the code DCRoundSwitch uses to change the switch value. animated
is YES
and ignoreControlEvents
is NO
.
- (void)setOn:(BOOL)newOn animated:(BOOL)animated ignoreControlEvents:(BOOL)ignoreControlEvents
{
BOOL previousOn = self.on;
on = newOn;
ignoreTap = YES;
[CATransaction setAnimationDuration:0.014];
knobLayer.gripped = YES;
// setup by turning off the manual clipping of the toggleLayer and setting up a layer mask.
[self useLayerMasking];
[self positionLayersAndMask];
[CATransaction setCompletionBlock:^{
[CATransaction begin];
if (!animated)
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
else
[CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
CGFloat minToggleX = -toggleLayer.frame.size.width / 2.0 + toggleLayer.frame.size.height / 2.0;
CGFloat maxToggleX = -1;
if (self.on)
{
toggleLayer.frame = CGRectMake(maxToggleX,
toggleLayer.frame.origin.y,
toggleLayer.frame.size.width,
toggleLayer.frame.size.height);
}
else
{
toggleLayer.frame = CGRectMake(minToggleX,
toggleLayer.frame.origin.y,
toggleLayer.frame.size.width,
toggleLayer.frame.size.height);
}
if (!toggleLayer.mask)
{
[self useLayerMasking];
[toggleLayer setNeedsDisplay];
}
[self positionLayersAndMask];
knobLayer.gripped = NO;
[CATransaction setCompletionBlock:^{
[self removeLayerMask];
ignoreTap = NO;
// send the action here so it get's sent at the end of the animations
if (previousOn != on && !ignoreControlEvents)
[self sendActionsForControlEvents:UIControlEventValueChanged];
}];
[CATransaction commit];
}];
}
CATransaction
see anything wrong with it? – AustinUISwitch
in the final product, becauseDCRoundSwitch
provides customization options thatUISwitch
does not. – Austinbegin
andcommit
. The layers/mask are all part of DCRoundSwitch (github.com/domesticcatsoftware/DCRoundSwitch), which shares a parent view with the container. Changes to one view's mask should not affect changes to a sibling's alpha, should they? – Austin