简体   繁体   中英

Drag/Drop between 3 ScrollViews [ iOS ]

Is it possible to have 3 scroll views lets say the middle is the main where is user find objects inside, then user can drag any item and drop it to the right scroll view or left scroll view so that's object will be removed from the middle and dropped either right or left depends on the uses and suppose the user dropped it to the left thus system should add new object to the left scroll view but when the user remove this object and return back to the main (middle) preferred to dropped and added in the same old index if the object was number 2 then the object will added and dropped at index 2 ..etc.

Could you help on this to make this functionality ?

Let's call the items model1 ... model3 ;

@property (nonatomic, strong) NSMutableArray *mainArray;
@property (nonatomic, strong) NSMutableArray *leftArray;
@property (nonatomic, strong) NSMutableArray *rightArray;

@property (nonatomic, strong) UIScrollView *mainView;
@property (nonatomic, strong) UIScrollView *leftView;
@property (nonatomic, strong) UIScrollView *rightView;

You need make a model class such as ItemModel inherit UIView or NSObject , in ItemModel.h make some property(what property you need and a property about which UIScrollView contains this model)

First, put all models in the mainView's datasource mainArray , display models in your self.view and call self.view bringSubviewToFront: and give all models a UIPanGestureRecognizer and a method to respond to UIPanGestureRecognizer so you can drag them.

This is the most important part:

Because you want all models to be contained by three UIViews, you must calculate the models frame between self.view.frame and the three UIView frames relationship. In fact, all modelViews is contained by self.view , but when visually displayed, all modelViews is contained within UIScrollViews.

Second, you already have the frame of three UIView , when you drag model, you can get CGPoint about the location of the drag, then call the method CGRectContainsPoint(CGRect rect, CGPoint point) , so you can get any view that contains the point you drag.

For exexample, you drag model1 from mainView to leftView , you can get a YES from CGRectContainsPoint(leftview.frame, point) , then remove the model1 from mainArray and put model1 in leftArray , meanwhile you need to re-layout the three UIViews .

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

here is my share URL for the gif: https://pan.baidu.com/s/1slFBva9

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