简体   繁体   中英

Drag UIView without disabling its child controls iPhone/iPad

I have a UIView control (white rectangle in image)

在此处输入图像描述

Moreover I am able to drag that control...

在此处输入图像描述

And when I press the button I load a subview which is another nib that I created and I placed random controls in it to illustrate my point...

在此处输入图像描述

If you guys are interested in finding out how I placed that nib file in that UIView control take a look at this question. I don't thing you have to read it in order to understand my question.

Anyways the problem when loading that nib file is that I can no longer drag the top UIView. Because of this I changed:

在此处输入图像描述

for:

在此处输入图像描述

in the UIView of the subview. In otherwords the UIView of the nib file that I am placing in the UIView that has the white background.

and when I did that I was able to drag the control but the controls inside the subview no longer works. I have also tried placing the touchesMoved method in the subview instead but when I do that the application behaves strange. Plus the purpose of placing the nib file in a UIView control was to avoid repeating the same drag funcionality on several nib files.

I actually need to create an application like a power point presentation and I need to change the slide as the user slides the UIView and if it's cords are less than x for example then I load the next slide (nib file) in that uiview controller. Maybe there is a simpler way of doing what I need but if I get this drag to work I am done cause I would just have to do that functionality just once.

You should leave the UserInteractionEnabled flag on for your subview if you want it to respond to events.

One way to achieve this would be to do your dragging using a UIGestureRecognizer.

UIPanGestureRecognizer is perfect for this ( UIGestureRecognizer at apple )

Basically you'd attach the gesturerecognizer to the view which you want to pan then adjust it's position in the callbacks it provides.

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(handlePanGesture:)];
panGesture.minimumNumberOfTouches = 1;
[draggableSubview addGestureRecognizer:panGesture];
[panGesture release];

Then in the handlePanGesture method you figure out how far the user panned using the translationInView method of the recognizer which it gets passed and translate the subview accordingly.

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