2013-03-15 94 views
0

我需要实现自定义UISwitch,为此我使用RCSwitch类(感谢Ray Wenderlich和Robert Chin)。RCSwitch激活一次触摸两次

因此,我添加了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并评论switch时,正常的UISwitch只能启动switchSwitched方法一次。

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

回答

0

我得到了它, RCSwitch.m有这样的方法:

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

最后一行森达行动,因为这种方法对开关的推出两次。

+0

霍德你解决了吗?我不认为对图书馆最后一行的评论是解决方案,对吧? – Jan 2013-07-28 21:31:33

+0

其实我评论了最后一行,但毕竟在最终版本我放弃使用RCSwitch,所以也许有另一种解决方案。顺便说一句:如果你评论最后一行开关仍然正常工作。 – lvp 2013-07-31 10:09:38