RCSwitch一次激活两次

时间:2013-03-15 09:00:47

标签: ios uiswitch

我需要实现自定义UISwitch,为此我使用RCSwitch类(感谢Ray Wenderlich和Robert Chin)。

所以我将RCSwitch类添加到我的项目中,连接图形,它看起来很棒,但是!它不太好用。看看这个:

此代码:

//@interface
@property (nonatomic, strong) RCSwitchOnOff *onSwitch;

//implementation
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.onSwitch = [[RCSwitchOnOff alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 39.5, self.view.frame.size.height/2 - 150, 80, 35)];
    [self.onSwitch addTarget:self action:@selector(switchSwitched:) forControlEvents:UIControlEventTouchUpInside];

//    self.defaultSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 39.5, self.view.frame.size.height/2 - 150, 80, 35)];
//    [self.defaultSwitch addTarget:self action:@selector(switchSwitched:) forControlEvents:UIControlEventTouchUpInside];

.
.
.
}

- (void)switchSwitched:(id)sender
{
    NSLog(@"switch touched!");
}

在NSLog中导致此问题

2013-03-15 09:56:54.575 Secret-Project[1190:c07] switch touched!
2013-03-15 09:56:54.576 Secret-Project[1190:c07] switch touched! 

对于一个用户触摸方法,switchSwitched会触发两次! 当我取消对defaultSwitch的uncommnet并对Switch进行注释时,正常的UISwitch只启动switchSwitched方法一次。

到底是什么?这里有人有同样的问题吗?

1 个答案:

答案 0 :(得分:0)

我明白了, RCSwitch.m有这个方法:

- (void)performSwitchToPercent:(float)toPercent
{
    endDate = [NSDate dateWithTimeIntervalSinceNow:fabsf(percent - toPercent) * animationDuration];
    percent = toPercent;
    [self setNeedsDisplay];
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    [self sendActionsForControlEvents:UIControlEventTouchUpInside]; 
}

最后一行senda动作,因为这种切换方法正在启动两次。

相关问题