Xcode iOS按下按钮然后向上拖动第二个按钮 [英] Xcode iOS push down button and drag then up on a second button

查看:185
本文介绍了Xcode iOS按下按钮然后向上拖动第二个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我不想将1添加到整数。这只会在我按下 UIButton 然后在另一个 UIButton 上释放我的手指时完成。 拖动组合。什么是最简单的方法,我可以在组合中进行 IBAction ?这可以通过触摸坐标完成,也可以只用 UIButtons IBActions

Lets say I wan't to add 1 to an integer. This will only be done when I push down on a UIButton and then release my finger on another UIButton. A drag combo. Whats the easiest way I can make an IBAction occur out of a combo? This could be done with touch coordinates or maybe just UIButtons and IBActions.

推荐答案

尝试将您想要触摸的按钮实现为触摸向下,触摸内部和向上触摸按钮。

Try implementing the button you wish to touch down on as a "Touch Down", "Touch Up Inside", and "Touch Up Outside" button.

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

UIButtons can respond to many differing types of events Touch Cancel Touch Down Touch Down Repeat Touch Drag Enter Touch Drag Exit Touch Drag Inside Touch Drag Outside Touch Up Inside Touch Up Outside

您可以为每个按钮实现不同的操作代码以获得最佳效果控制你想要的任何东西。简单的情况仅使用上面提到的2。

You can implement different action code for each of these for each button for best control of whatever you wish. The simplistic case only uses the 2 I mentioned above.

此代码已经过测试并且可以工作

THIS CODE HAS BEEN TESTED AND DOES WORK

In你的ViewController头文件(这是我的):

In your ViewController header file (here was mine):

@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中,连接向下按钮(一个你希望成为你的原始手指按下按钮)上面的正确行动。

In your .xib, connect the down button (the one you wish to be your original finger pressed button) to the proper actions above.

在你的ViewController.m文件中

In your ViewController.m file

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

这篇关于Xcode iOS按下按钮然后向上拖动第二个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆