在iPhone中使用点按计数控制拖动移动

时间:2011-08-25 15:02:45

标签: iphone cocoa-touch touch drag

我有一个自定义按钮类.. 我希望能够通过点击计数来控制此按钮。 当用户只点击一次时,该按钮将弹出一个评论框。 当用户点击两次或点击一次并按住按钮超过2秒时,按钮将开始拖动.. 我将这些行添加到我的自定义按钮类以捕获拖动事件..

[self addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
[self addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside];
[self addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];


-(void)dragBegan:(UIControl *)c withEvent:ev {
[delegate customDragBegan:c withEvent:ev];

我试过这个功能:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

};

但是当触摸结束时,dragEnds不会发射..我有点搞砸......

现在,我可以正确拖动按钮..只需要弄清楚如何控制第一次尝试... 2点击拖动,1点击另一个功能..

提前谢谢..

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:(一点点新手,但它解决了我的问题)
我使用触摸事件..当触摸开始时我检查触摸计数..如果它等于2然后我调用我的代理自定义拖动功能..并将我的isDraggable BOOL设置为是..所以在触摸结束功能我可以确定是否我应该在委托中调用自定义拖动功能..

我希望这对像我这样的人有所帮助..

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touchSample = [[event allTouches] anyObject];
switch ([touchSample tapCount]) {
    case 1:
        isDraggable = NO;
        NSLog(@"SHOW COMMENT");
        break;
    case 2:
        isDraggable = YES;
        NSLog(@"START DRAG");
        [delegate customDragBegan:self withEvent:event];
        break;   
    default:

        break;
}


};
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (isDraggable) {
    [delegate customDragMoving:self withEvent:event];
};


};

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

if (isDraggable) {
    [delegate customDragEnded:self withEvent:event];
};

};