簡體   English   中英

將觸摸轉移到 UIImageView 的副本

[英]Transferring touches to copy of UIImageView

我一直在嘗試將 UIImageViews 從 UITableView 拖放到另一個視圖中。

我的一切工作都達到了一定程度。 我繼承了 UIPanGestureRecognizer 並將這個自定義識別器附加到我加載到表視圖中的所有 UIImageViews。

然后我覆蓋識別器的 touchesMoved 方法,除其他外,將臨時視圖添加到應用程序鍵 window,然后將所選 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 的背景顏色設置為白色)。 如果我抬起手指再次觸摸復制的圖像視圖,我當然可以隨意在屏幕上拖動它。 但是我找不到將現有觸摸無縫轉移到新的 object 的方法。

有任何想法嗎?

我認為您可以在 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;

}

}

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM