让我说我不想把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];
}发布于 2012-06-20 14:38:57
//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上,所以您的结果可能会有所不同,但是这应该会让您朝着正确的方向前进。
发布于 2013-03-04 20:31:30
另一种方法是使用UITapGestureRecognizer和UIPanGestureRecognizer并跟踪按钮中的位置。我发现这个更容易读懂。(实际上,我最终使用了UILabels,因为我不需要任何进一步的UIButton行为。)
https://stackoverflow.com/questions/11121827
复制相似问题