Xcode的iOS按下button,然后再按住第二个button
可以说我不想把1加到一个整数。 这只会当我按下一个UIButton
,然后释放我的手指在另一个UIButton
。 拖动组合。 什么是最简单的方法,我可以做一个IBAction
出现一个组合? 这可以通过触摸坐标来完成,也可以只使用UIButtons
和IBActions
。
如何创build一个2键组合与IBActions
尝试实现您想要触摸的button,如“触摸下”,“触摸内部”和“触摸外部”button。
UIButtons可以响应许多不同types的事件触摸取消触摸向下触摸向下重复触摸拖动input触摸拖动退出触摸拖动内部拖动拖动外部触摸内部触摸外部
您可以为每个button实现不同的操作代码,以便最好地控制任何您想要的操作。 简单的情况只使用上面提到的2。
此代码已经过testing并可以工作
在您的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文件中,将向下button(您希望成为原始手指按下button的那个button)连接到上面的正确操作。
在你的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]; }
//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,所以你的结果可能会有所不同,但这应该让你朝着正确的方向前进。
另一种方法是使用UITapGestureRecognizer
和UIPanGestureRecognizer
并跟踪button的位置。 我发现这更可读。 (我实际上结束了使用UILabels,因为我不需要任何进一步的UIButton
行为。)
//创build一个button,添加目标
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.............."); }
对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"); } }