1

假设我不想给整数加 1。只有当我按下 aUIButton然后松开手指时才会这样做UIButton拖拽组合。什么是我可以IBAction从组合中发生的最简单方法?这可以通过触摸坐标来完成,也可以只用UIButtonsand来完成IBActions

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

4

5 回答 5

5

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

UIButtons 可以响应许多不同类型的事件 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。

此代码已经过测试并且有效

在您的 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

在您的 .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];
}
于 2012-06-20T14:37:11.653 回答
3
 //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,所以你的结果可能会有所不同,但这应该会让你朝着正确的方向前进。

于 2012-06-20T14:38:57.707 回答
1

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

于 2013-03-04T20:31:30.200 回答
1

// 创建一个按钮,添加目标

UIButton *cloudButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cloudButton setImage:[UIImage imageNamed:@"notification_add.png"] forState:UIControlStateNormal];
        [cloudButton setFrame:CGRectMake(0, 0, 30, 30)];
        [cloudButton addTarget:self action:@selector(dragBegan:withEvent: )
           forControlEvents: UIControlEventTouchDown];
        [cloudButton addTarget:self action:@selector(dragMoving:withEvent: )
           forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
        [cloudButton addTarget:self action:@selector(dragEnded:withEvent: )
           forControlEvents: UIControlEventTouchUpInside |
         UIControlEventTouchUpOutside];

        [self.view addSubview:cloudButton];

//事件方法

- (void) dragBegan: (UIControl *) c withEvent:ev
{
    NSLog(@"dragBegan......");
}

- (void) dragMoving: (UIControl *) c withEvent:ev
{
    NSLog(@"dragMoving..............");
}
- (void) dragEnded: (UIControl *) c withEvent:ev
{
    NSLog(@"dragEnded..............");
}
于 2014-08-08T07:36:11.037 回答
1

ViewDidLoad对方法中的第一个 UIButton 执行以下操作

    [button1 addTarget:self action:@selector(dragMoving:withEvent: )
              forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];

    [button1 addTarget:self action:@selector(dragEnded:withEvent: )
              forControlEvents: UIControlEventTouchUpInside |
     UIControlEventTouchUpOutside];

dragMoving方法中

- (void)dragMoving:(UIControl *)c withEvent:event{

    NSSet* touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[button2 superview]];


        if ( CGRectContainsPoint(button2.frame, currentPoint) ) {
            NSLog(@"over second button); 
        } 
    }

至于dragEnded方法:

- (void) dragEnded:(UIControl *)c withEvent:event{

    NSSet* touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[self.button1 superview]];
        if ( CGRectContainsPoint(button2.frame, currentPoint) ) {
            NSLog(@"ended on button 2");
            //add 1 to the integer


        }
    else {
        NSLog(@"not on button 2");
    }
}
于 2015-10-24T23:55:40.643 回答