Xcode iOS按下按钮然后向上拖动第二个按钮

Com*_*sky 1 touch uibutton drag ipad ios

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

如何创建一个2按钮组合 IBActions

tru*_*cks 5

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

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
Run Code Online (Sandbox Code Playgroud)

在.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];
}
Run Code Online (Sandbox Code Playgroud)