问题
I have done following code for animation inside a selector method of custom UISwitch DCRoundSwitch.
if ([[[App.remindersArray objectAtIndex:0] objectAtIndex:3]isEqualToString:@"YES"]){
[firstReminderOnOffButton setSelected:YES];
[swtchDailyReminder setOn:YES];
imgviewDailyReminder.image=[UIImage imageNamed:@"nDailyReminder_On_1.png"];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDidStopSelector:@selector(animateFadingIn)];
[UIView setAnimationDelegate:self];
imgviewDailyReminderAnimation.alpha = 1.0;
[UIView commitAnimations];
}
else
{
[firstReminderOnOffButton setSelected:NO];
[swtchDailyReminder setOn:NO];
imgviewDailyReminder.image=[UIImage imageNamed:@"xDailyReminder_OFF.png"];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDidStopSelector:@selector(animateFadingIn)];
[UIView setAnimationDelegate:self];
imgviewDailyReminderAnimation.alpha = 0.0;
[UIView commitAnimations];
}
The problem is animation is working properly when above code is called from normal UISwitch,but not working when called from DCRoundSwitch.
Also tried to resolve by using UIView block animation....but still facing the problem.
Please guide me.
回答1:
The issue is in DCRoundSwitch's
animation block. No CATransaction
is created in the completion block of setOn:animated:ignoreControlEvents:
resulting in [UIView animateWithDuration...]
methods not working properly when called in response to the switch's value being changed.
To solve the problem, just change:
if (previousOn != on && !ignoreControlEvents)
[self sendActionsForControlEvents:UIControlEventValueChanged];
To:
if (previousOn != on && !ignoreControlEvents)
{
[CATransaction begin];
[CATransaction setDisableActions:NO];
[self sendActionsForControlEvents:UIControlEventValueChanged];
[CATransaction commit];
}
That should do it
回答2:
i have used this below code. which works correctly.
Please follow up this thread
https://github.com/domesticcatsoftware/DCRoundSwitch/issues/12
U can just go to the Class which is using this DCRoundSwitch , on that class's dealloc method put this line.
- (void)dealloc
{
[self.MyDCRoundSwitch removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
[super dealloc];
}
来源:https://stackoverflow.com/questions/12585923/uiview-animation-not-working-when-used-with-dcroundswitch