开发者

UIButton behind UIScrollView

开发者 https://www.devze.com 2023-04-02 18:31 出处:网络
I have a UIButton behind a UIScrollView. The scroll\'s background is transparent and the button is in the lower ri开发者_C百科ght corner. So it is visible.

I have a UIButton behind a UIScrollView. The scroll's background is transparent and the button is in the lower ri开发者_C百科ght corner. So it is visible.

The problem is that the UIButton does not respond to any touch events when it is below the scroll. What would it require to make it respond to touch events in this scenario?

Thank you


You should use the +touchesBegan method to pass on the touches to the next object.
Here is a example of how you would do this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
printf("MyButton touch Began\n");
[self.nextResponder touchesBegan:touches withEvent:event]; 
}

Heres some more info on the responder chain:

  1. A Bit About the Responder Chain (http://iphonedevelopment.blogspot.com)

  2. Cocoa Application Competencies for iOS: Responder Object (http://developer.apple.com)


I had this problem today, after much time investigating is pretty difficult to achieve, because you can lose the dragging ability in exchange for the buttons click event.

However: I ended up with this, a UIScrollView subclass whose receives the button or buttons to which it is interested in catching the event.

The key is to use [(UIControl*)view sendActionsForControlEvents: UIControlEventTouchUpInside];, because calling touchesBegan method not always works as expected. This won't change the GUI as you press the button but you can implement that as needed in a custom method.

@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

The counting on the gestureRecognizers is to avoid the pan gesture that could trigger the button when no real tap is made.


Maybe a better approach is to put a UIView subclass (instead of a UIButton) as subview on UIScrollView and add a UITapGestureRecognizer on them.


For newbies like me who come to look for help, here is how you do it in Storyboard: drag the button to the same level as the scrollView, and make sure the button is BELOW the scrollView - the order matters! The lower the position, the topper the view layer is


You can try this. U need to add the button to the subview after you add your scrollview.

....

[myView addSubview:myScrollView];

.....

[myView addSubview:myButton];

If you do it in IB, u just need to drag you button below your scrollview...


If the UIButton is behind the UIScrollView, then it cannot respond to touches because there is a UIScrollView in the way! Put the UIButton on the UIScrollView or shrink the UIScrollView so that it no longer obstructs the button.


Actually you can do this, I have done it on the Mac, had to do with passing down the events. Search online you should be able to find some pointers. I remember it took me a while to solve it a while back.

0

精彩评论

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