如何在iphone中點擊按鈕后獲取觸摸事件?

[英]how to get touch event after a button click in iphone?


I have a UIButton in my project and after a button click I am showing a UIView on the top of that button in such a way that the button remains hidden that time. I want to get a touchesmoved event on that time when I keep that button pressed and move finger over the overlapped UIView. As far as I did is like the following:

我的項目中有一個UIButton,點擊一下按鈕后,我在該按鈕頂部顯示一個UIView,使按鈕保持隱藏狀態。當我按住該按鈕並將手指移到重疊的UIView上時,我希望得到一個觸動移動的事件。就我而言,如下所示:

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

 UITouch *touch = [touches anyObject];

    if ([touch view] == uiview)
    {

        NSLog(@"Touches moved");
    }

but it doesnt get called when I keep the button pressed that is hidden when the UIView comes up and the uiview goes hidden when I release the button. Any suggestion please?

但當我按下按鈕時它不會被調用,當UIView出現時隱藏按鈕,當我釋放按鈕時uiview被隱藏。有什么建議嗎?

2 个解决方案

#1


3  

I don't quiet understand what your trying to do but I would suggest you use UIGestureRecognizers and add Gestures Recognizers to your button.

我不清楚你想要做什么,但我建議你使用UIGestureRecognizers並將Gestures Recognizers添加到你的按鈕。

Try using this code. I've used this in a card game i developed. moving cards around using long press gestures. Hope i helps.

嘗試使用此代碼。我在我開發的紙牌游戲中使用過它。使用長按手勢移動卡片。希望我有所幫助。

 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
 }

#2


0  

You need to set view.userInteractionEnabled = NO for your overlapped UIView. This will send action to button.

您需要為重疊的UIView設置view.userInteractionEnabled = NO。這會將動作發送到按鈕。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/10/10/c03afa63db9a56c4906f40961aa180ff.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com