Xcode iOS push down button and drag then up on a second button

后端 未结 5 1421
悲&欢浪女
悲&欢浪女 2021-01-15 20:06

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.

5条回答
  •  抹茶落季
    2021-01-15 20:47

    Do the following for the first UIButton in the ViewDidLoad method

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

    In the dragMoving method

    - (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); 
            } 
        }
    

    As for the dragEnded method:

    - (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");
        }
    }
    

提交回复
热议问题