2011-09-06 68 views
4

我在UIScrollView後面有一個UIButton。滾動的背景是透明的,按鈕位於右下角。所以它是可見的。UIScrollView後面的UIButton

的問題是,當它是滾動下面的UIButton沒有任何觸摸事件。在這種情況下,響應觸摸事件需要什麼?

謝謝

+1

爲什麼不把UIButton放在UIScrollView的頂部? – titaniumdecoy

回答

-9

其實你可以做到這一點,我已經做了在Mac上,曾與傳承做事件。在網上搜索你應該能夠找到一些指針。我記得我花了一段時間才解決它。

+1

有史以來最差的答案。作者和評論者都不知道如何使用SO? – bitwit

-3

如果UIButtonUIScrollView的背後,則無法觸摸,因爲在途中UIScrollView迴應!將UIButton放在UIScrollView或縮小UIScrollView,使其不再妨礙按鈕。

+0

但是,當滾動移動時,我需要將按鈕固定並放在滾動條的後面 – vgr

+1

@vgr:嗯,你也不能吃你的蛋糕。前視圖阻止後視圖。這就是它的工作原理。 – PengOne

+0

但它有完全透明的背景 – vgr

0

也許更好的做法是把一個UIView子類(而不是一個UIButton)作爲UIScrollView的子視圖並對其添加UITapGestureRecognizer。

+0

如果您將UIView作爲UIScrollView的子視圖,那麼它將隨滾動一起移動。我需要按鈕/ UIView靜止。 – vgr

+0

好吧,只能嘗試[parentViewOfButton bringViewToFront:uibutton]; – D33pN16h7

-2

你可以試試這個。在添加滾動視圖後,需要將按鈕添加到子視圖。

....

[MyView的addSubview:myScrollView];

.....

[myView addSubview:myButton];

如果您在IB做到這一點,你剛剛需要拖動滾動型的你之下...按鈕

4

您應該使用+的touchesBegan方法對觸摸傳遞給下一個對象。
這裏是你會怎麼做這樣的例子:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
printf("MyButton touch Began\n"); 
[self.nextResponder touchesBegan:touches withEvent:event]; 
} 

繼承人的響應鏈一些更多的信息:

  1. A Bit About the Responder Chain (http://iphonedevelopment.blogspot.com)

  2. Cocoa Application Competencies for iOS: Responder Object (http://developer.apple.com)

-1

對於像我這樣的新手誰前來尋求幫助,這裏是你如何在故事板做到這一點:拖動按鈕到同一水平滾動視圖,並確保該按鈕下方的滾動視圖 - 訂單事宜!位置越低,禮帽視圖層是

+0

問題是關於通過讓按鈕位於較低層 – DeFrenZ

2

我今天有這個問題,很多時間調查後,相當難以實現的,因爲你可以在交換失去拖拽能力的按鈕單擊事件。

不過:我結束了這一點,一個UIScrollView子類,其接收按鈕或按鍵,它是有意捕捉事件。

關鍵是要用[(UIControl *)view sendActionsForControlEvents:UIControlEventTouchUpInside];,因爲調用touchesBegan方法不總是按預期方式工作。這不會在您按下按鈕時更改GUI,但您可以根據需要在自定義方法中實現該功能。

@implementation ForwardScrollView 

@synthesize responders = _responders; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     _touchesEnabled = YES; 
    } 
    return self; 
} 


- (id)initWithCoder:(NSCoder *)aDecoder { 
    if (self = [super initWithCoder: aDecoder]) { 
     _touchesEnabled = YES; 
    } 
    return self; 
} 


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    return _touchesEnabled; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    _touchesEnabled = NO; 
    UIWindow *window = [UIApplication sharedApplication].delegate.window; 
    for(UITouch *touch in touches) { 
     CGPoint point = [touch locationInView:self]; 
     point = [window convertPoint:point fromView:self]; 
     UIView *view = [window hitTest:point withEvent:event]; 

     UITouch* touch = [touches anyObject]; 
     UIGestureRecognizer* recognizer; 
     if ([touch gestureRecognizers].count > 0) { 
      recognizer = [touch gestureRecognizers][0]; 
     } 

     if ((!recognizer || ![recognizer isMemberOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")]) && [_responders containsObject: view]) { 
      [(UIControl*)view sendActionsForControlEvents: UIControlEventTouchUpInside]; 
     } 

    } 
    _touchesEnabled = YES; 

} 

@end 

對gestureRecognizers的計數是爲了避免在沒有真正的敲擊時可能觸發按鈕的平移手勢。