与DCRoundSwitch一起使用时,UIView动画无法正常工作
我在自定义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]; }
问题是当从正常的UISwitch调用上面的代码时动画正常工作,但是从DCRoundSwitch调用时不能正常工作。
还试图通过使用UIView块动画来解决….但仍然面临问题。
请指导我。
问题出在DCRoundSwitch's
动画块中。 在CATransaction
的完成块中没有创建setOn:animated:ignoreControlEvents:
导致[UIView animateWithDuration...]
方法在响应交换机的值被更改时被调用时无法正常工作。
要解决这个问题,只需更改:
if (previousOn != on && !ignoreControlEvents) [self sendActionsForControlEvents:UIControlEventValueChanged];
至:
if (previousOn != on && !ignoreControlEvents) { [CATransaction begin]; [CATransaction setDisableActions:NO]; [self sendActionsForControlEvents:UIControlEventValueChanged]; [CATransaction commit]; }
应该这样做
我使用下面的代码。 哪个工作正常。
请跟进这个post
https://github.com/domesticcatsoftware/DCRoundSwitch/issues/12
你可以直接去使用这个DCRoundSwitch的Class,在该类的dealloc方法中放入这一行。
- (void)dealloc { [self.MyDCRoundSwitch removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; [super dealloc]; }