2

我尝试使用 InterfaceBuilder 将 UISegmentedControl 的 touchDownRepeat 传递到其父 viewController 中的 IBAction,但是无论您连续点击 UISegmentedControl 多少次,IBAction 都不会触发。如果 UISegmentedControl 什么都不做,为什么它们会在 InterfaceBuilder 的弹出菜单中显示此输出?你如何让它做它应该做的事情?(对不起,如果这是一个菜鸟问题。)

另外,UISegmentedControl 的每个段可以直接作为对象引用吗?如何对其进行子类化?如果没有,为什么不呢,什么是好的解决方法?

我想让它双击一个段将启动一个视图以编辑该段表示的模式。谢谢。

4

3 回答 3

0

为什么不在UIButtons分段控件之上添加?您可以使用以下命令检测对它们的双击UIControlEventTouchDownRepeat,然后使用以下命令手动选择分段控件:

- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment

于 2013-09-18T15:53:37.343 回答
0
-(void)initTouchesRecognizer{
    DLog(@"");
    recognizer = [[UIGestureRecognizer alloc] init];
    [self addGestureRecognizer:recognizer];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    DLog(@"");

    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches) {

    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   DLog(@"");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

这是手势识别器的代码。手势识别器是为 UIView 设计的,所以实际上每个视图只能有一种手势识别器类型。您可以在同一页面上对不同的事情使用单击、双击、三次点击、滑动等,但是让两个相同类型的人做两件不同的事情是一项壮举,因为它们的设计初衷不是那样。您应该使用按钮,就像前面的答案所说的那样。我试图做同样的事情并最终改用按钮,结果证明这更方便。即使您可以让手势识别器按照您想要的方式工作,这也是非常困难和乏味的。UIButton 的代码要短得多。

编辑 手势识别器通常用于在点击 UIView 时使键盘消失等事情;像这样的东西。它不应该用作页面上的一组自定义按钮。

编辑#2 这是你如何让它识别手势,以便它调用适当的函数:

-(void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(0, 0, 100, 100);
    [self.view addSubview:button];
    UIGestureRecognizer *swipe=[[UIGestureRecognizer alloc]initWithTarget:button     action:@selector(detectSwipe)];
    [button addGestureRecognizer:swipe];
}

你必须初始化它并告诉它去哪里。

于 2013-09-18T16:00:21.917 回答
0

其实我终于想出了这个解决方案。子类化 UISegmentedControl。里面:

- (customUISegmentedControl *)init {
    NSTimeInterval interval = 0.0;
    _lastSameTouchTime = [NSDate dateWithTimeIntervalSince1970:interval];
    return self;
}
+ (BOOL)isIOS7 {
    static BOOL isIOS7 = NO;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSUInteger deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
        if (deviceSystemMajorVersion >= 7) {
            isIOS7 = YES;
        }
        else {
            isIOS7 = NO;
        }
    });
    return isIOS7;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
    [super touchesBegan:touches withEvent:event];
    if (![[self class] isIOS7]) {
        // before iOS7 the segment is selected in touchesBegan
        if (previousSelectedSegmentIndex == self.selectedSegmentIndex) {
            // if the selectedSegmentIndex before the selection process is equal to the selectedSegmentIndex
            // after the selection process the superclass won't send a UIControlEventValueChanged event.
            // So we have to do this in here.
            NSTimeInterval diff = [[NSDate date] timeIntervalSinceDate:_lastSameTouchTime];
            _lastSameTouchTime = [NSDate date];
            if(diff <= 1.0)
                NSLog(@"doubletapped!");
            diff = [[NSDate date] timeIntervalSinceDate:_changedTime];
            if(diff <= 1.0)
                NSLog(@"doubletapped!");
        }
        else {
            _changedTime = [NSDate date];
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
    [super touchesEnded:touches withEvent:event];
    if ([[self class] isIOS7]) {
        // on iOS7 the segment is selected in touchesEnded
        if (previousSelectedSegmentIndex == self.selectedSegmentIndex) {
            NSTimeInterval diff = [[NSDate date] timeIntervalSinceDate:_lastSameTouchTime];
            _lastSameTouchTime = [NSDate date];
            if(diff <= 1.0)
                NSLog(@"doubletapped!");
            diff = [[NSDate date] timeIntervalSinceDate:_changedTime];
            if(diff <= 1.0)
                NSLog(@"doubletapped!");

        }
        else {
            _changedTime = [NSDate date];
        }
    }
}

基本上现在用NSLog(@"doubletapped!")发生双击发生时执行任何需要发生的代码替换代码:D 当然,为_changedTime 和_lastSameTouchTime 声明相关的NSDate 属性。

于 2015-05-05T01:36:24.273 回答