【问题标题】:Xcode iOS push down button and drag then up on a second buttonXcode iOS 按下按钮然后向上拖动第二个按钮
【发布时间】:2012-06-22 17:22:06
【问题描述】:

假设我不想给整数加 1。这只会在我按下UIButton 然后松开手指在另一个UIButton 上时完成。 拖动组合。IBAction 从组合中出现的最简单方法是什么?这可以通过触摸坐标完成,也可以只使用UIButtonsIBActions

如何使用IBActions 创建一个 2 键组合

【问题讨论】:

  • 我已经用完全测试过的代码更新了我的答案,它确实有效!!!

标签: ios ipad uibutton touch drag


【解决方案1】:

ViewDidLoad 方法中的第一个 UIButton 执行以下操作

    [button1 addTarget:self action:@selector(dragMoving:withEvent: )
              forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];

    [button1 addTarget:self action:@selector(dragEnded:withEvent: )
              forControlEvents: UIControlEventTouchUpInside |
     UIControlEventTouchUpOutside];

dragMoving 方法中

- (void)dragMoving:(UIControl *)c withEvent:event{

    NSSet* touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[button2 superview]];


        if ( CGRectContainsPoint(button2.frame, currentPoint) ) {
            NSLog(@"over second button); 
        } 
    }

至于dragEnded方法:

- (void) dragEnded:(UIControl *)c withEvent:event{

    NSSet* touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[self.button1 superview]];
        if ( CGRectContainsPoint(button2.frame, currentPoint) ) {
            NSLog(@"ended on button 2");
            //add 1 to the integer


        }
    else {
        NSLog(@"not on button 2");
    }
}

【讨论】:

    【解决方案2】:

    // 创建按钮,添加目标

    UIButton *cloudButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [cloudButton setImage:[UIImage imageNamed:@"notification_add.png"] forState:UIControlStateNormal];
            [cloudButton setFrame:CGRectMake(0, 0, 30, 30)];
            [cloudButton addTarget:self action:@selector(dragBegan:withEvent: )
               forControlEvents: UIControlEventTouchDown];
            [cloudButton addTarget:self action:@selector(dragMoving:withEvent: )
               forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
            [cloudButton addTarget:self action:@selector(dragEnded:withEvent: )
               forControlEvents: UIControlEventTouchUpInside |
             UIControlEventTouchUpOutside];
    
            [self.view addSubview:cloudButton];
    

    //事件方法

    - (void) dragBegan: (UIControl *) c withEvent:ev
    {
        NSLog(@"dragBegan......");
    }
    
    - (void) dragMoving: (UIControl *) c withEvent:ev
    {
        NSLog(@"dragMoving..............");
    }
    - (void) dragEnded: (UIControl *) c withEvent:ev
    {
        NSLog(@"dragEnded..............");
    }
    

    【讨论】:

      【解决方案3】:

      另一种方法是使用UITapGestureRecognizerUIPanGestureRecognizer 并跟踪按钮中的位置。我发现这更具可读性。 (实际上我最终使用了 UILabel,因为我不需要任何进一步的 UIButton 行为。)

      【讨论】:

        【解决方案4】:

        尝试将您希望触摸的按钮实现为“Touch Down”、“Touch Up Inside”和“Touch Up Outside”按钮。

        UIButtons 可以响应许多不同类型的事件 触摸取消 触地 触地重复 触摸拖动输入 触摸拖动退出 触摸内部拖动 触摸向外拖动 修饰内部 在外面修饰

        您可以为每个按钮实现不同的操作代码,以便最好地控制您想要的任何内容。简单的案例只使用了我上面提到的2个。

        此代码已经过测试并且有效

        在您的 ViewController 头文件中(这是我的):

        @interface ViewController : UIViewController{
            IBOutlet UIButton * upButton;    // count up when finger released button
            IBOutlet UIButton * downButton;
            IBOutlet UILable * score;
            BOOL isButtonDown;
            unsigned int youCounter;
        }
        
        -(IBAction)downButtonDown:(id)sender;
        -(IBAction)downButtonUpInside:(id)sender;
        -(IBAction)downButtonDragOutside:(id)sender event:(UIEvent *)event;
        -(IBAction)downButtonUpOutside:(id)sender event:(UIEvent *)event;
        
        @end
        

        在您的 .xib 中,将向下按钮(您希望成为原始手指按下的按钮)连接到上面的正确操作。

        在您的 ViewController.m 文件中

        -(void)viewDidLoad{
            [super viewDidLoad];
        
            isButtonDown = NO;
            youCounter   = 0;
        }
        
        -(IBAction)downButtonDown:(id)sender{
            isButtonDown = YES;
        }
        
        -(IBAction)downButtonUpInside:(id)sender{
            isButtonDown = NO;
        }
        
        -(IBAction)downButtonDragOutside:(id)sender event:(UIEvent *)event{
            NSArray theTouches = [[event allTouches] allObjects];
        
            [downButton setHighlighted:YES];
        
            if(YES == [upButton pointInside:[[theTouches objectAtIndex:0] locationInView:upButton] withEvent:event]){
                [upButton setHighlighted:YES];
            }else{
                [upButton setHighlighted:NO];
            }
        }
        
        -(IBAction)downButtonUpOutside:(id)sender event:(UIEvent *)event{
            if(YES == [upButton pointInside:[[theTouches objectAtIndex:0] locationInView:upButton] withEvent:event]){
                youCounter++;
                score.text = [NSString stringWithFormat:@"Score = %d", youCounter];
            }
        
            [downButton setHighlighted:NO];
            [upButton setHighlighted:NO];
        }
        

        【讨论】:

        • Touch Up Inside 仅在按钮内部开始触摸时发送。如果您从按钮外部拖动,然后在按钮内部松开,则不会在该控件上调用 Touch Up Inside
        • 是的,我正在尝试思考这个问题,因为必须有比 Jeremy 提供的答案更简单的方法,我开始这样思考,然后尝试在 Apple Button API 的范围内思考! !! +1 杰里米
        • @comradsky - 应该正在路上(或者那里:-)!!!还要查看控制台输出,以更好地了解正在发生的事情!!!
        • 中间的变量是什么意思?
        • 我已经向您发送了另一封电子邮件,其中包含您所要求的所有代码的新版本。
        【解决方案5】:
         //Initalize a BOOL variable to know if you started the touch in the right place.
         BOOL correctStart = NO;
        
         //Get the location of the first touch, if its in the first button make correctStart = YES.
         -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
              NSSet *allTouches = [event allTouches];
                   for (UITouch *touch in allTouches) {
                        if ([touch locationInView:button1.view]) {
                             correctStart = YES;
                        }
                   }
         }
        
         -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
              NSSet *allTouches = [event allTouches];
                   for (UITouch *touch in allTouches) {
                        if (([touch locationInView:button2.view]) && (correctStart == YES)) {
                             anInteger++;
                        }
                   }
              correctStart = NO;
         }
        

        我没有尝试这段代码,因为我不在我的 mac,所以你的结果可能会有所不同,但这应该会让你朝着正确的方向前进。

        【讨论】:

        • @comradsky 正如我所说,我目前不在我的 Mac 上。下班回家后,如果您仍有问题,我会为您解决我的代码问题。
        • 非常感谢杰里米。需要明确的是,我在 .h 中将按钮设置为 ibactions,而您的代码当然属于 .m。所以我会尝试,但我可能需要你的帮助。但我学到了很多。再次感谢
        • 这段代码也不起作用,locationInView 返回一个 CGPoint,而不是布尔值!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2020-01-23
        • 1970-01-01
        • 2012-12-02
        • 2020-12-12
        • 1970-01-01
        相关资源
        最近更新 更多