首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Xcode iOS按下按钮,然后拖到第二个按钮上

Xcode iOS按下按钮,然后拖到第二个按钮上
EN

Stack Overflow用户
提问于 2012-06-20 14:29:30
回答 5查看 10.4K关注 0票数 1

让我说我不想把1加到整数上。只有当我按下一个UIButton,然后在另一个UIButton上松开手指时,才能做到这一点。--拖动组合体。,我最简单的方法是从组合中生成IBAction?这可以通过触摸坐标完成,也可以只使用UIButtonsIBActions

如何创建与IBActions的2按钮组合

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-06-20 14:37:11

试着将你想要触摸的按钮实现为“向下触摸”、“触摸内部”和“向外触摸”按钮。

UIButtons可以响应许多不同类型的事件,点击取消触摸,向下触摸,重复触摸拖动,进入触摸拖动出口,触摸外部拖动,外部触摸,向上触摸

您可以为每个按钮实现不同的操作代码,以便对任何您想要的操作进行最佳控制。简化的情况只使用上面提到的2。

这段代码已经过测试,并能正常工作。

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

代码语言:javascript
运行
复制
@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文件中

代码语言:javascript
运行
复制
-(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];
}
票数 5
EN

Stack Overflow用户

发布于 2012-06-20 14:38:57

代码语言:javascript
运行
复制
 //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上,所以您的结果可能会有所不同,但是这应该会让您朝着正确的方向前进。

票数 3
EN

Stack Overflow用户

发布于 2013-03-04 20:31:30

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11121827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档