如何在iphone中单击按钮后获取触摸事件?

时间:2013-10-10 18:57:05

标签: iphone ios objective-c uiview

我的项目中有一个UIButton,点击一下按钮后,我在该按钮的顶部显示UIView,使按钮保持隐藏状态。当我按住该按钮并将手指移到重叠的touchesmoved上时,我希望获得UIView事件。就我而言,如下所示:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

 UITouch *touch = [touches anyObject];

    if ([touch view] == uiview)
    {

        NSLog(@"Touches moved");
    }

但当按下按钮时,它不会被调用,当UIView出现时隐藏按钮,当我释放按钮时uiview被隐藏。有什么建议吗?

2 个答案:

答案 0 :(得分:3)

我不清楚你想要做什么,但我建议你使用UIGestureRecognizers并在你的按钮上添加手势识别器。

尝试使用此代码。我在我开发的纸牌游戏中使用过它。使用长按手势移动卡片。希望我有所帮助。

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
 [longPress setDelegate:self];
 [YOUR_BUTTON addGestureRecognizer:longPress];

- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {

UIView *view = sender.view;

CGPoint point = [sender locationInView:view.superview];

if (sender.state == UIGestureRecognizerStateBegan){ 

      // GESTURE STATE BEGAN

}
else if (sender.state == UIGestureRecognizerStateChanged){

     //GESTURE STATE CHANGED/ MOVED

    CGPoint center = view.center;
    center.x += point.x - _priorPoint.x;
    center.y += point.y - _priorPoint.y;
    view.center = center;

    // This is how i drag my views
  }

 else if (sender.state == UIGestureRecognizerStateEnded){

      //GESTURE ENDED
 }

答案 1 :(得分:0)

您需要为重叠的UIView设置view.userInteractionEnabled = NO。 这会将动作发送到按钮。