大佬教程收集整理的这篇文章主要介绍了iphone – UIScrollview子视图以外的剪辑边界没有接收到触摸,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这很好用,但我现在的问题是UIScrollView的子视图没有得到任何触摸事件 – 只有主UIScrollView才有.
在下面的示例中,如果包含hitTest代码,则滚动视图正确滚动,一次分页一列并且可以看到其所有内容 – 但内部滚动视图不会接收触摸事件.
如果我删除了hitTest代码,那么只有第一个子scrollview接收到触摸,并且可以看到它的所有内容 – 但主要的scrollview不会在非剪切区域中进行触摸.
我怎么@L_197_9@这个问题?
//========================================= // 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,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
更新
通过执行以下操作,我将触摸转发到内部视图,但肯定必须有更好的方法吗?
- (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]; } }
以上是大佬教程为你收集整理的iphone – UIScrollview子视图以外的剪辑边界没有接收到触摸全部内容,希望文章能够帮你解决iphone – UIScrollview子视图以外的剪辑边界没有接收到触摸所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。