UIView animation not working when used with DCRoundSwitch

别说谁变了你拦得住时间么 提交于 2019-12-12 10:23:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!