我正在尝试根据 DCRoundSwitch 对象的值隐藏或显示视图,但 UIView 动画块没有动画 - 更改会立即应用。我还尝试使用 CATransaction 为视图层设置动画,但这些更改也立即应用了。如果相同的动画块被移动到另一个位置(例如,viewWillAppear:),动画效果很好。
非常感谢任何帮助确定动画为什么不起作用的帮助。谢谢!
这是动画代码:
- (void)switchValueChanged:(id)sender
{
[UIView animateWithDuration:0.33
animations:^{
self.containerView.alpha = self.switchView.isOn ? 1 : 0;
}];
}
作为参考,这里是 DCRoundSwitch 用于更改开关值的代码。animated
是YES
和ignoreControlEvents
是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];
}];
}