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

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

问题描述

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

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.

推荐答案

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

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

p>

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

在您的 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天全站免登陆