简体   繁体   中英

Handle touch gestures in two views

I have two UIScrollViews one over another in the hierarchy. I name it: scrollView1 which is the view from the front and scrollView2 which is the view from behind.

The scrollView1 has a transparent view in CGRect(0,0,320,568) and some content in CGRect(0,568,320,300) . If the user scroll the screen vertically he needs to see the view that stars from y = 568 .

The scrollView2 contains some UIImages displayed horizontally, each image starts at origin.x = screenWidth * imageIndex , where imageIndex starts from 0 . So the scrollView2 have to scroll horizontally through images , also it has some UIButtons in the content .

How can I do to be able to scroll the scrollView1 vertically but in the same time be able to scroll scrollView2 horizontally and also trigger UIButtonEventTouchUpInside on its buttons. The method - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event doesn't help me because I have to choose if current view handle or not the event.

Thanks for your help!

I think you must have to check on which view Gesture is added.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch 
{            
 // enter code here

}

For:

How can I do to be able to scroll the scrollView1 vertically but in the same time be able to scroll scrollView2 horizontally

You need to implement UISwipeGestureRecognizer as:

  - (void)didSwipe:(UISwipeGestureRecognizer*)swipe{

if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
    NSLog(@"Swipe Left"); // Implement Horizontal Scroll
} else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
    NSLog(@"Swipe Right"); // Implement Horizontal Scroll
} else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
    NSLog(@"Swipe Up"); // Implement Vertical Scroll
} else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
    NSLog(@"Swipe Down"); // Implement Vertical Scroll
}
}

For:

UIButtonEventTouchUpInside

I am not sure what you are trying to implement, but if your views are overlapping ( one-view behind other), than you need to bring target view to front by changing its YourTargetView.zPosition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM