iPhone/iOS: How to implement Drag and Drop for subviews of a Scrollview?

后端 未结 5 1204
盖世英雄少女心
盖世英雄少女心 2020-12-09 20:50

I am having an horizontal scrollview in an UIViewController, where i have many images in small sizes. I am keeping the images in scrollview because the images a

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 21:41

    For a similar problem, I made UIScrollView subclass like the following: PoliteScrollView passes touch messages to it's subviews when it determines that they are being dragged.

    @interface PoliteScrollView : UIScrollView
    
    // number of pixels perpendicular to the scroll views orientation to make a drag start counting as a subview drag
    // defaults to 1/10 of the scroll view's width or height, whichever is smaller
    @property(nonatomic,assign) CGFloat subviewDraggingThreshold;
    // yes when a subview is being dragged
    @property(nonatomic,assign) BOOL isDraggingSubview;
    // the subview being dragged
    @property(nonatomic,strong) UIView *draggingSubview;
    
    @end
    
    @protocol PoliteScrollViewDelegate 
    @optional
    - (void)scrollView:(PoliteScrollView *)scrollView subviewTouchesBegan:(NSSet *)touches;
    - (void)scrollView:(PoliteScrollView *)scrollView subviewTouchesMoved:(NSSet *)touches;
    - (void)scrollView:(PoliteScrollView *)scrollView subviewTouchesEnded:(NSSet *)touches;
    @end
    

    The key design idea is that dragging in a scroll view is ambiguous. Is the user scrolling or dragging a subview? PoliteScroll view handles this by providing two things: (1) a notion of orientation (horizontal if it's longer than it is wide, vertical otherwise), and (2) a threshold distance for what constitutes a drag in the direction perpendicular to it's orientation. (defaults to 1/10 the width or height).

    I pasted this and several other files are in a paste bin, containing the following:

    • PoliteScrollView .h and .m
    • DraggableImageView.h and .m - that changes it's position when it gets touch messages.
    • ViewController.m - that demonstrates thetwo in combination.

    To combine these in a project, paste the paste bin into files appropriately named, add a storyboard with a PoliteScrollView (be sure to set it's delegate), add in some images (the ViewController tries to add puppy0.jpeg to puppy4.jpeg.

提交回复
热议问题