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

Xcode iOS push down button and drag then up on a second button(Xcode iOS 按下按钮然后向上拖动第二个按钮)
本文介绍了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 按下按钮然后向上拖动第二个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)