将触摸传输到 UIImageView 的副本

发布于 2024-11-17 18:37:43 字数 1066 浏览 2 评论 0原文

我经历了漫长的旅程,试图将 UIImageViews 从 UITableView 拖放到另一个视图。

我让一切都按部就班。我对 UIPanGestureRecognizer 进行子类化,并将此自定义识别器附加到我加载到表视图中的所有 UIImageView。

然后,我重写识别器的 TouchMoved 方法,除其他外,将临时视图添加到应用程序键窗口上,然后将所选 UIImageView 的副本添加到该临时视图上,如下所示:

UIView *dragAndDropView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
        dragAndDropView.backgroundColor = [UIColor whiteColor];

        [[UIApplication sharedApplication].keyWindow addSubview: dragAndDropView]; 

        ElementImageView * sourceImageView = (ElementImageView*)self.view;

        ElementImageView *newImageView =  [[ElementImageView alloc] initWithImage:sourceImageView.image];
        newImageView.userInteractionEnabled = YES;
        newImageView.frame = [self.view convertRect:sourceImageView.frame toView:dragAndDropView];

        [dragAndDropView addSubview:newImageView];

作为此代码的结果,我确实得到了我的dragAndDropView 顶部的源图像视图的副本(我可以看出,因为我将dragAndDropView 的背景颜色设置为白色)。如果我抬起手指并再次触摸复制的图像视图,我当然可以根据需要将其在屏幕上拖动。但我找不到一种方法将现有的触摸无缝转移到新对象。

有什么想法吗?

I've been a long journey trying to drag and drop UIImageViews from a UITableView to another view.

I have everything working to a point. I subclass UIPanGestureRecognizer and attach this custom recognizer to all the UIImageViews I load into the table view.

I then override the recognizer's touchesMoved method to, among other things, add a temporary view onto the applications key window, then add a copy of the selected UIImageView onto this temporary view, as follows:

UIView *dragAndDropView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
        dragAndDropView.backgroundColor = [UIColor whiteColor];

        [[UIApplication sharedApplication].keyWindow addSubview: dragAndDropView]; 

        ElementImageView * sourceImageView = (ElementImageView*)self.view;

        ElementImageView *newImageView =  [[ElementImageView alloc] initWithImage:sourceImageView.image];
        newImageView.userInteractionEnabled = YES;
        newImageView.frame = [self.view convertRect:sourceImageView.frame toView:dragAndDropView];

        [dragAndDropView addSubview:newImageView];

As a result of this code, I do indeed get a copy of the source image view on top of my dragAndDropView (I can tell because I set the backgroundcolor of the dragAndDropView to white). And if I lift my finger and touch the copied image view again, I can of course drag it around the screen as I wish. But I can't find a way to seamlessly transfer the existing touch to the new object.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-11-24 18:37:43

我认为您可以在 UIPanGestureRecognizer 处理程序上执行以下操作:

-(void) handlePan:(UIPanGestureRecognizer *)recognizer  {

if (recognizer.state == UIGestureRecognizerStateBegan) {

    // the initial position
    initialPanPositionX = self.transform.tx;
    initialPanPositionY = self.transform.ty;

    // create a copy of the imageview
    panImage = [[UIImageView alloc] initWithImage:imageView.image];
    panImage.frame = imageView.frame;

    [self.view addSubview:panImage];

}
else if (recognizer.state == UIGestureRecognizerStateChanged) {

    if (!startedMoving) {
        startedMoving = YES;

        [UIView beginAnimations:nil context:NULL]; {
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:0.2f];

            // an effect to increase a copy a little bit when start dragging
            CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);

            panImage.bounds = CGRectApplyAffineTransform(panImage.bounds, zt);

        }
        [UIView commitAnimations];

    }

    // translation
    CGPoint point = [recognizer translationInView:self];
    panImage.transform = CGAffineTransformMakeTranslation(initialPanPositionX + point.x, initialPanPositionY + point.y);


}
else if (recognizer.state == UIGestureRecognizerStateEnded ) {

    startedMoving = NO;

    // drop the imageView if it's inside the droppable view


    [panImage release], panImage = nil;

}

}

希望有帮助。

I think you could do the following on your UIPanGestureRecognizer handler:

-(void) handlePan:(UIPanGestureRecognizer *)recognizer  {

if (recognizer.state == UIGestureRecognizerStateBegan) {

    // the initial position
    initialPanPositionX = self.transform.tx;
    initialPanPositionY = self.transform.ty;

    // create a copy of the imageview
    panImage = [[UIImageView alloc] initWithImage:imageView.image];
    panImage.frame = imageView.frame;

    [self.view addSubview:panImage];

}
else if (recognizer.state == UIGestureRecognizerStateChanged) {

    if (!startedMoving) {
        startedMoving = YES;

        [UIView beginAnimations:nil context:NULL]; {
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:0.2f];

            // an effect to increase a copy a little bit when start dragging
            CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);

            panImage.bounds = CGRectApplyAffineTransform(panImage.bounds, zt);

        }
        [UIView commitAnimations];

    }

    // translation
    CGPoint point = [recognizer translationInView:self];
    panImage.transform = CGAffineTransformMakeTranslation(initialPanPositionX + point.x, initialPanPositionY + point.y);


}
else if (recognizer.state == UIGestureRecognizerStateEnded ) {

    startedMoving = NO;

    // drop the imageView if it's inside the droppable view


    [panImage release], panImage = nil;

}

}

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文