我在UIScrollView後面有一個UIButton。滾動的背景是透明的,按鈕位於右下角。所以它是可見的。UIScrollView後面的UIButton
的問題是,當它是滾動下面的UIButton沒有任何觸摸事件。在這種情況下,響應觸摸事件需要什麼?
謝謝
我在UIScrollView後面有一個UIButton。滾動的背景是透明的,按鈕位於右下角。所以它是可見的。UIScrollView後面的UIButton
的問題是,當它是滾動下面的UIButton沒有任何觸摸事件。在這種情況下,響應觸摸事件需要什麼?
謝謝
其實你可以做到這一點,我已經做了在Mac上,曾與傳承做事件。在網上搜索你應該能夠找到一些指針。我記得我花了一段時間才解決它。
有史以來最差的答案。作者和評論者都不知道如何使用SO? – bitwit
你可以試試這個。在添加滾動視圖後,需要將按鈕添加到子視圖。
....
[MyView的addSubview:myScrollView];
.....
[myView addSubview:myButton];
如果您在IB做到這一點,你剛剛需要拖動滾動型的你之下...按鈕
您應該使用+的touchesBegan方法對觸摸傳遞給下一個對象。
這裏是你會怎麼做這樣的例子:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
printf("MyButton touch Began\n");
[self.nextResponder touchesBegan:touches withEvent:event];
}
繼承人的響應鏈一些更多的信息:
對於像我這樣的新手誰前來尋求幫助,這裏是你如何在故事板做到這一點:拖動按鈕到同一水平滾動視圖,並確保該按鈕下方的滾動視圖 - 訂單事宜!位置越低,禮帽視圖層是
問題是關於通過讓按鈕位於較低層 – DeFrenZ
我今天有這個問題,很多時間調查後,相當難以實現的,因爲你可以在交換失去拖拽能力的按鈕單擊事件。
不過:我結束了這一點,一個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的計數是爲了避免在沒有真正的敲擊時可能觸發按鈕的平移手勢。
爲什麼不把UIButton放在UIScrollView的頂部? – titaniumdecoy