可拖动的UIButton不响应UIControlEvents

时间:2011-09-18 07:55:40

标签: ios uibutton touchesmoved uicontrolevents

创建可拖动 UIButton时,拖动事件可以正常工作,但是,我无法弄清楚当按钮为UIControlEvents时没有响应的原因只是按下而不是拖动。

创建按钮

DraggableButton * camera = [DraggableButton buttonWithType:UIButtonTypeCustom];
[camera setFrame:CGRectMake(160,5,149,40)];
[camera setImage: [UIImage imageWithContentsOfFile:cameraMode] forState:UIControlStateNormal];
[camera setTitle: @"Camera" forState:UIControlStateNormal];  // never gets called.
[camera addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: camera];

可拖动按钮

@interface DraggableButon : UIButton
{
@private float deltaX;
@private float deltaY;
    CGPoint startLocation;
    BOOL    dragging;
}
@end
@implementation DraggableButon
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesMoved:touches withEvent:event];

    if ( ! draggable ) return;

    dragging = YES;
    camera.enabled = NO;
    flash.enabled = NO;

    // Calculate offset
    CGPoint pt = [[[event allTouches] anyObject] locationInView:buttonView];
    deltaX = pt.x - startLocation.x;
    deltaY = pt.y - startLocation.y;

    float xx;
    if ( IPAD ) { xx = buttonView.center.x + deltaX; } else { xx = 160; }
    CGPoint newcenter = CGPointMake(xx, buttonView.center.y + deltaY);

    // Set new location
    buttonView.center = newcenter;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesBegan:touches withEvent:event];

    if ( ! draggable ) return;

    // Calculate and store offset, and pop view into front if needed
    CGPoint pt = [[[event allTouches] anyObject] locationInView:buttonView];
    startLocation = pt;
    [[self superview] bringSubviewToFront:buttonView];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{        
    [super touchesEnded:touches withEvent:event];

    dragging = YES;
    if ( dragging )
    {
        camera.enabled = YES;
        flash.enabled = YES;

        //NSLog(@"touchesEnded: buttonView.frame.origin.x: %3.2f, buttonView.frame.origin.y: %3.2f",buttonView.frame.origin.x,buttonView.frame.origin.y);
        rectFromString = [NSString stringWithFormat:@"{{%3.2f,%3.2f},{320,50}}",buttonView.frame.origin.x,buttonView.frame.origin.y]; 
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: SETTINGS_FILE];
        [dict setObject:rectFromString forKey:@"kCGRectFromString"];
        [dict writeToFile: SETTINGS_FILE atomically:YES];
    }else{
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    [super touchesCancelled:touches withEvent:event];
}
@end

2 个答案:

答案 0 :(得分:1)

因为您覆盖触摸事件而未将其发送到super,因此未调用该操作。您可以添加一个新变量(让我们称之为dragged)。在touchesBegan:将其设置为“否”,将touchesMoved:设为“是”,将设置为“touchesEnded:”设置为“否”{<1}}

[self sendActionsForControlEvents:UIControlEventTouchUpInside];

答案 1 :(得分:0)

您是否已调试以及是否调用了触摸事件功能? 简单的按下操作应该在touchesEnded: withEvent:

中实现

由于它是从UIButton继承的,您是否尝试过像addTarget: action: forControlEvents:那样直接添加操作事件?