可拖动的 UIButton 不响应 UIControlEvents

发布于 2024-12-04 22:04:29 字数 2806 浏览 0 评论 0原文

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

When creating a Draggable UIButton, the dragging events work as they should, however, I am unable to figure out why the UIControlEvents do not respond when the button is simply pressed and not dragged.

Creating the Button

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];

Draggable Button

@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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-12-11 22:04:29

该操作不会被调用,因为您覆盖了触摸事件并且没有将它们进一步发送到 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

the action is not called because you overwrite the touch events and you're not sending them further to super. You can add a new variable (let's call it dragged). Set this to NO at touchesBegan:, to YES at touchesMoved: and at touchesEnded:, if it's set to NO call [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
层林尽染 2024-12-11 22:04:29

是否调试过,触摸事件函数是否被调用?
简单的按下操作应该在 touchesEnded: withEvent: 中实现

,因为它继承自 UIButton,您是否尝试过像 addTarget: action: forControlEvents 一样直接添加操作事件: ?

Have you debug and whether the touch event functions are invoked?
And Simple pressed action should be implemented in touchesEnded: withEvent:

Since it is inherited from UIButton,have you tried to add action event directly likeaddTarget: action: forControlEvents:?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文