ios - 可拖动的 UIButton 不响应 UIControlEvents

标签 ios uibutton touchesmoved uicontrolevents

创建 Draggable 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

最佳答案

该操作未被调用,因为您覆盖了触摸事件并且没有将它们进一步发送到 super。您可以添加一个新变量(我们称之为draged)。在 touchesBegan: 处将其设置为 NO,在 touchesMoved:touchesEnded: 处将其设置为 YES,如果设置为 NO,则调用 [self sendActionsForControlEvents:UIControlEventTouchUpInside];

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

    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)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    dragging=YES;

    if ( ! draggable ) return;

    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)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{        
    if(!dragging){  // or if(!dragging||!draggable)
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
        return;
    }

    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];
}
@end

关于ios - 可拖动的 UIButton 不响应 UIControlEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7460204/

相关文章:

ios - 如果我将手指从我的节点移开并进入 GameView,为什么我的节点会停止旋转?

iOS:使用 .png 在 View 中着色

ios - 经纬度点的奇怪位置

ios - 自定义 UITableViewCell 中的 EXC_BAD_ACCESS

iOS UIButton 标题在 UITableViewCell 中不断恢复

ios - 如何将iOS键盘 "go"按钮与UIButton关联?

ios - 在流上为 UIButton 设置动画

ios - 为什么 UITabBarController 向 viewWillTransitionToSize 中的隐藏 View 提供错误的大小?

ios - 在 sqlite 数据库中插入行

xcode - [IOS SDK]-touchesBegan 以特定对象开始?