Touch and pull down a view

后端 未结 3 2010
终归单人心
终归单人心 2020-12-28 11:16

I have a uiview at the top of the interface (below the status bar) that only the bottom part of it is shown.

Actually, I want to make the red uiview to slide down

3条回答
  •  青春惊慌失措
    2020-12-28 11:45

    No needs to find a workaround of drag-n-drop. An UIScrollView can do it without any performance loss brought by listening on touches.

    pulldown

    @interface PulldownView : UIScrollView
    
    @end
    
    @implementation PulldownView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (!self) {
            return self;
        }
        self.pagingEnabled = YES;
        self.bounces = NO;
        self.showsVerticalScrollIndicator = NO;
        [self setBackgroundColor:[UIColor clearColor]];
        double pixelsOutside = 20;// How many pixels left outside.
        self.contentSize = CGSizeMake(320, frame.size.height * 2 - pixelsOutside);
        // redArea is the draggable area in red.
        UIView *redArea = [[UIView alloc] initWithFrame:frame];
        redArea.backgroundColor = [UIColor redColor];
        [self addSubview:redArea];
        return self;
    }
    
    // What this method does is to make sure that the user can only drag the view from inside the area in red.
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        if (point.y > height)
        {
            // Leaving useless touches to the views under it.
            return nil;
        }
        return [super hitTest:point withEvent:event];
    }
    
    @end
    

    How to use:
    1. Initialize an instance of PulldownView.
    2. Add any content you want to display to the instance using [addSubview:].
    3. Hide the area in red.

    [pulldownView setContentOffset:CGPointMake(0, heightOfTheView - pixelsOutside)];
    

    This is a simple example. You can add any features to it like adding a titled button bar on the bottom of the draggable area to implement click-n-drop, or adding some method to the interface to reposition it by the caller.

提交回复
热议问题