gpt4 book ai didi

iphone - UIScrollview subview 超出剪切范围未接收触摸

转载 作者:行者123 更新时间:2023-12-03 18:35:37 24 4
gpt4 key购买 nike

我有一个 UIScrollView,我将其设置为一次滑动一列(每页两列) - 通过将框架设置为 View 实际宽度的一半,将 ClipToBounds 设置为 NO 并使用 hitTest 声明外部区域框架属于 UIScrollView(请参见下面的示例)。

这很好用,但我现在的问题是 UIScrollView 的 subview 没有得到任何触摸事件 - 只有主 UIScrollView 得到。

在下面的示例中,如果包含 hitTest 代码,则 ScrollView 会正确滚动,一次分页一列,并且可以看到其所有内容 - 但内部 ScrollView 不会接收触摸事件。

如果我删除 hitTest 代码,则只有第一个子 ScrollView 接收触摸,并且可以看到其所有内容 - 但主 ScrollView 不会在非剪辑区域中获得触摸。

我该如何解决这个问题?

示例:

//=========================================
// UIScrollViewEx
// Just in order to log touches...
//=========================================

@interface UIScrollViewEx : UIScrollView {}
@end

@implementation UIScrollViewEx
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touches Began (0x%08X)", (unsigned int)self);
}
@end

//=========================================
// UIViewEx
// Dummy class - sets subview as hit target
// just to demonstrate usage of non-clipped
// content
//=========================================

@interface UIViewEx : UIView {}
@end

@implementation UIViewEx
- (UIView *) hitTest:(CGPoint) point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
return [self.subviews objectAtIndex:0];
}
return nil;
}
@end

//=========================================
// MainClass
// Any UIViewEx based class which returns
// the UIScrollView child on hittest
//=========================================

@implementation MyClass

- (UIColor*) randomColor
{
int r = arc4random() % 100;
int g = arc4random() % 100;
int b = arc4random() % 100;
return [UIColor colorWithRed:(0.01 * r) green:(0.01 * g) blue:(0.01 * b) alpha:1.0];
}

- (void) loadScrollviews
{
// Set frame to half of actual width so that paging will swipe half a page only
CGRect frame = CGRectMake(0, 0, self.bounds.size.width / 2, 400);

// Main scrollview
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:frame];
[scrollview setBackgroundColor:[UIColor greenColor]];
[scrollview setPagingEnabled:YES];
[scrollview setClipsToBounds:NO];

// Create smaller scrollviews inside it - each one half a screen wide
const int numItems = 5;
for(int i = 0; i < numItems; ++i) {
frame.origin.x = frame.size.width * i;
UIScrollView *innerScrollview = [[UIScrollViewEx alloc] initWithFrame:frame];
[innerScrollview setContentSize:CGSizeMake(frame.size.width, 1000)];
[innerScrollview setBackgroundColor:[self randomColor]];
[scrollview addSubview:innerScrollview];
[innerScrollview release];
}

[scrollview setContentSize:CGSizeMake(numItems * frame.size.width, frame.size.height)];

[self addSubview:scrollview];
}

@end

更新
我通过执行以下操作将触摸转发到内部 View ,但肯定有更好的方法吗?

- (UIView *) hitTest: (CGPoint) pt withEvent: (UIEvent *) event 
{
if(CGRectContainsPoint(self.bounds, pt))
{
UIScrollView *scrollview = [self.subviews objectAtIndex:0];
CGPoint scrollViewpoint = [scrollview convertPoint:pt fromView:self];

for(UIView *view in scrollview.subviews) {
if(CGRectContainsPoint(view.frame, scrollViewpoint)) {
return view;
}
}
return scrollview;
}
else {
return [super hitTest:pt withEvent:event];
}
}

最佳答案

这可能有效:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {
UIView* child = nil;
if ((child = [super hitTest:point withEvent:event]) == self)
return self.scrollView;
return child;
}

但是,如果 subview 超出 ScrollView 范围,则不会触发事件,并且此函数返回 self.scrollView。

关于iphone - UIScrollview subview 超出剪切范围未接收触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com