Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone/iOS:如何为Scrollview的子视图实现拖放?_Iphone_Ios_Uiscrollview_Drag And Drop - Fatal编程技术网

iPhone/iOS:如何为Scrollview的子视图实现拖放?

iPhone/iOS:如何为Scrollview的子视图实现拖放?,iphone,ios,uiscrollview,drag-and-drop,Iphone,Ios,Uiscrollview,Drag And Drop,我在UIViewController中有一个水平滚动视图,其中有许多小尺寸的图像。我将图像保留在scrollview中,因为图像更多,所以用户可以水平滚动并选择图像。但是,问题是,我必须选择一个图像并拖放到UIViewController视图。但是,由于图像在scrollview中,因此将图像拖放到UIViewcontroller的视图不起作用,也检测不到触摸事件。 请注意:如果我没有scrollview,但只是将图像保存在UIViewcontroller的视图中,将图像拖放到同一屏幕上,效果非

我在
UIViewController
中有一个水平滚动视图,其中有许多小尺寸的图像。我将图像保留在scrollview中,因为图像更多,所以用户可以水平滚动并选择图像。但是,问题是,我必须选择一个图像并拖放到
UIViewController
视图。但是,由于图像在scrollview中,因此将图像拖放到
UIViewcontroller
的视图不起作用,也检测不到触摸事件。 请注意:如果我没有scrollview,但只是将图像保存在
UIViewcontroller的视图中,将图像拖放到同一屏幕上,效果非常好

当我需要滚动查看和拖放图像时,如何解决此问题,请提供任何建议/帮助?

嗨,格西

我不会直接向您提供代码,但会给出如何管理这些代码的想法

您可以通过这种方式进行管理,当您当时在scrollView中触摸到您的对象时,或者当您通过拖动移动该对象时,禁用scroll by
myScroll.scrollEnabled=NO


然后,当使用endTouch时,您可以通过
myScroll.scrollEnabled=YES启用滚动这样你就可以管理你的对象在滚动中移动,希望你有逻辑


以下是演示代码:。它的逻辑与
禁用
滚动查看
触摸移动:
启用
滚动查看
触摸移动:
的逻辑相同。对于类似的问题,我创建了UIScrollView子类,如下所示:PoliteScrollView在确定正在拖动触摸消息时将触摸消息传递给它的子视图

@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 <NSObject>
@optional
- (void)scrollView:(PoliteScrollView *)scrollView subviewTouchesBegan:(NSSet *)touches;
- (void)scrollView:(PoliteScrollView *)scrollView subviewTouchesMoved:(NSSet *)touches;
- (void)scrollView:(PoliteScrollView *)scrollView subviewTouchesEnded:(NSSet *)touches;
@end
@界面视图:UIScrollView
//垂直于滚动视图方向的像素数,以使拖动开始作为子视图拖动计数
//默认为滚动视图宽度或高度的1/10,以较小者为准
@属性(非原子,赋值)CGFloat子视图DraggingThreshold;
//拖动子视图时为“是”
@属性(非原子,赋值)BOOL isDragingSubview;
//正在拖动的子视图
@属性(非原子,强)UIView*DragingSubView;
@结束
@协议视图委托
@可选的
-(void)scrollView:(礼貌scrollView*)scrollView子视图触摸开始:(NSSet*)触摸;
-(void)scrollView:(礼貌scrollView*)scrollView子视图触摸移动:(NSSet*)触摸;
-(void)scrollView:(PoliteScrollView*)scrollView子视图触控Send:(NSSet*)触控;
@结束
关键的设计思想是在滚动视图中拖动是不明确的。用户是否正在滚动或拖动子视图?“滚动视图”通过提供两件事来处理此问题:(1)方向的概念(如果方向比宽度长,则为水平方向,否则为垂直方向),以及(2)在垂直于方向上构成阻力的阈值距离。(默认为宽度或高度的1/10)

我粘贴了此文件,其他几个文件位于中,包含以下内容:

  • 请查看.h和.m
  • DragableImageView.h和.m-当它收到触摸信息时,会改变它的位置
  • m-演示了两者的组合
要在项目中组合这些内容,请将粘贴箱粘贴到适当命名的文件中,添加带有滚动视图的情节提要(确保设置其代理),添加一些图像(ViewController尝试将puppy0.jpeg添加到puppy4.jpeg。

Getsy

请尝试拖放对象的代码:

-(void)dragAndDropWithGesture {

UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:)];
[scrollViewAlfabeto addGestureRecognizer:downwardGesture];
    for (UIGestureRecognizer *gestureRecognizer in myscrollView.gestureRecognizers)
    {
       [gestureRecognizer requireGestureRecognizerToFail:downwardGesture];
    }
}

- (void) dragGestureChanged:(UIPanGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:scrollViewAlfabeto];

if (gesture.state == UIGestureRecognizerStateBegan) 
{

    [imageViewToMove removeFromSuperview];
    [self.view addSubview:imageViewToMove];

    UIView *draggedView = [myscrollView hitTest:point withEvent:nil];
    if ([draggedView isKindOfClass:[UIImageView class]])
    {
        imageViewToMove = (UIImageView*)draggedView;
    }
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
    imageToMove.center = point;
}
else if (gesture.state == UIGestureRecognizerStateEnded     ||
         gesture.state == UIGestureRecognizerStateCancelled ||
         gesture.state == UIGestureRecognizerStateFailed)
{
    // Determine if dragged view is in an OK drop zone
    // If so, then do the drop action, if not, return it to original location

    NSLog(@"point.x final:%f", point.x);
    NSLog(@"point.y final:%f", point.y);

    if (CGRectContainsPoint(goal.frame, point)){
        imageToMove.frame = CGRectMake(167, 159, 100, 100);
    }

    else{

        [imageToMove removeFromSuperview];
        [myscrollView addSubview:imageToMove];
        [imageToMove setFrame:CGRectMake(12, 38, 100, 100)];
        imageToMove = nil;



    }
}

}

但愿这段代码能帮上忙。

我以前在没有任何子类的情况下实现过这种行为

我使用了
UIScrollView
canCancelContentTouches=NO
,以确保子视图的句柄可以自己接触。如果子视图(在您的例子中是图像)被触摸,我会将视图从scrollview移到superview,并开始跟踪它的拖动。(必须在新的superview中计算正确的坐标,使其保持不变)

拖动完成后,我检查是否到达目标区域,否则我将其移回scrollview。如果不够详细,我可以发布一些代码


这是我的示例代码:

我创建了一个示例,演示了如何在两个或多个视图之间拖放:

我发现将手势识别器注册到与实际拖动视图不同的视图是一个好主意。这将确保即使拖动视图更改其“父”视图,手势仍会继续


也许它能给你一些启发

试试我的答案,我完全按照你想要的方式实现了。我添加了示例代码,如何实现在scrollview中拖拽和拖拽。你的示例链接无效。你能在我的邮件id上给我发这个代码吗我的邮件id是mohit27bisht@gmail.comWhy您是否使用UILongPressGestureRecognitor而不是UIPangEstureRecognitionR