让我说我不想把1加到整数上。只有当我按下一个UIButton,然后在另一个UIButton上松开手指时,才能做到这一点。--拖动组合体。,我最简单的方法是从组合中生成IBAction?这可以通过触摸坐标完成,也可以只使用UIButtons和IBActions。
如何创建与IBActions的2按钮组合
发布于 2012-06-20 14:37:11
试着将你想要触摸的按钮实现为“向下触摸”、“触摸内部”和“向外触摸”按钮。
UIButtons可以响应许多不同类型的事件,点击取消触摸,向下触摸,重复触摸拖动,进入触摸拖动出口,触摸外部拖动,外部触摸,向上触摸
您可以为每个按钮实现不同的操作代码,以便对任何您想要的操作进行最佳控制。简化的情况只使用上面提到的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];
}https://stackoverflow.com/questions/11121827
复制相似问题