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

后端 未结 5 1532
悲&欢浪女
悲&欢浪女 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:48

     //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;
     }
    

    I did not try this code as I am not at my mac, so your results may vary, but this should get you going in the right direction.

提交回复
热议问题