假设我不想在整数中加1。只有当我按下UIButton
然后将手指放在另一个UIButton
上时,才会执行此操作。 一个拖动组合。什么是最简单的方法,我可以让IBAction
出现在组合中?这可以通过触摸坐标完成,也可以只使用UIButtons
和IBActions
。
IBActions
答案 0 :(得分:5)
尝试将您想要触摸的按钮实现为“触摸向下”,“触摸内部”和“触摸外部”按钮。
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];
}
答案 1 :(得分:3)
//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上,因此您的结果可能会有所不同,但这应该会让您朝着正确的方向前进。
答案 2 :(得分:1)
另一种方法是使用UITapGestureRecognizer
和UIPanGestureRecognizer
并跟踪按钮中的位置。我发现这更具可读性。 (我实际上最终使用UILabels,因为我不需要任何进一步的UIButton
行为。)
答案 3 :(得分:1)
//创建一个按钮,添加目标
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..............");
}
答案 4 :(得分:1)
对ViewDidLoad
方法
[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");
}
}