开发者

UIScrollview and subviews touch events managing

I have the main wiev. I have placed a UIScrollView object as a subview of the main view. I have an extended class from UIView, which manage touch events. This class models a sort of transparent blackboard, in which the user can draw a path. I create an object out of my UIVIew extended class and I add it as a subview of the UIScrollView.

I carry out this process by using the Inspector and .xib file.

Then, no touch event gets to the UIScrollView object. No matter if I uncheck "User Interaction Enabled" checkbox for my UIView extended class in the Inspector. I've also tried with :

[[self nextResponder] touchesBegan:touches withevent:event];

or

[[self.superview nextResponder] touchesBegan:touches withevent:event];

or

 [super touchesBegan:touches withEvent:event];

or

[super touchesBegan:touches withEvent:event];

in the touchEvents methods of my extended UIView class. I haven't succeeded in getting touch events to the UIScrollView.

On the other hand, if I add scrollView and my extended UIView class 开发者_开发百科programatically, maintaining the same hierarchy, UIScrolView get touch events, but not its subview (my UIView extended class).

I don't want both UIScrollView and my UIView extended class to manage touch events at the same time. I just want to toggle between UIScrollView and my UIView extended class for listening to touch evebnts, by using a button for example.

Is it possible, in this case I am explaining, to select which, UIScrollView or my UIView extended class, should listen to touch events?


A UIScrollView has a scrollEnabled property. If you set this to YES, the scroll view will be scrollable. If not, the events will be forwarded up the responder chain. So try changing that property.

Read the docs for UIScrollView!

As for the difference between setting up in a nib versus XCode, your code setup and your nib version aren't quite matching some important way. I can't see your nib or your programmatic setup so can't help you much more with that.


You need to enable user interaction for scroll view:

[self.scrollView setUserInteractionEnabled:YES];

Add gestureRecognizer to subview:

for (int i = 0; i < pos.count; i++) {
        SCVerticalBarsChartBarView *bar = [[SCVerticalBarsChartBarView alloc]initWithBlaBlaBla];
        [bar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(someAction:)]];
        bar.tag = i;
        [self.scrollView addSubview:bar];            
    }

And detect the tapped view:

-(void)someAction:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
    if ([recognizer.view isKindOfClass:[SCVerticalBarsChartBarView class]]) {
        SCVerticalBarsChartBarView *tmpButt=(SCVerticalBarsChartBarView *)recognizer.view;
        NSLog(@"%d", tmpButt.tag);
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜