2017-11-03 7 views
0

iOSアプリケーション用にdrag and dropの対話を構築しています。透明な部分を含む画像をドラッグアンドドロップできるようにしたい。UIDragInteractionDelegate:dragInteraction(_:previewForLifting:session :)によって返されたドラッグプレビューに透明部分を表示する方法

ただし、ドラッグされたコンテンツの既定のプレビューは、私のアプリケーションの背景をカバーする不透明な白い背景の四角形です。私はAppleのコードサンプルAdopting Drag and Drop in a Custom ViewのようにUIDragInteractionDelegate方法 dragInteraction(_:previewForLifting:session:)を実装することでカスタムプレビューを作成するとき

、私の元画像の透明度はまだ私のプレビュー画像がまだ持つ矩形に表示されている意味、考慮されません不透明な白色の背景:

func dragInteraction(_ interaction: UIDragInteraction, previewForLifting item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? { 
    guard let image = item.localObject as? UIImage else { return nil } 

    // Scale the preview image view frame to the image's size. 
    let frame: CGRect 
    if image.size.width > image.size.height { 
     let multiplier = imageView.frame.width/image.size.width 
     frame = CGRect(x: 0, y: 0, width: imageView.frame.width, height: image.size.height * multiplier) 
    } else { 
     let multiplier = imageView.frame.height/image.size.height 
     frame = CGRect(x: 0, y: 0, width: image.size.width * multiplier, height: imageView.frame.height) 
    } 

    // Create a new view to display the image as a drag preview. 
    let previewImageView = UIImageView(image: image) 
    previewImageView.contentMode = .scaleAspectFit 
    previewImageView.frame = frame 

    /* 
     Provide a custom targeted drag preview that lifts from the center 
     of imageView. The center is calculated because it needs to be in 
     the coordinate system of imageView. Using imageView.center returns 
     a point that is in the coordinate system of imageView's superview, 
     which is not what is needed here. 
    */ 
    let center = CGPoint(x: imageView.bounds.midX, y: imageView.bounds.midY) 
    let target = UIDragPreviewTarget(container: imageView, center: center) 
    return UITargetedDragPreview(view: previewImageView, parameters: UIDragPreviewParameters(), target: target) 
} 

私は不透明ではないとプレビューを強制しようとしたが、それは助けていませんでした:

previewImageView.isOpaque = false 

私が取得できますかリフトプレビューの透明部分?

答えて

2

backgroundColorを上書きします。ドラッグアイテムのプレビューの背景の色はと定義されているため、です。

UIColor.clearに設定します。これは、グレースケール値とアルファ値がともに0.のカラーオブジェクトです。です。

let previewParameters = UIDragPreviewParameters() 
previewParameters.backgroundColor = UIColor.clear // transparent background 
return UITargetedDragPreview(view: previewImageView, 
          parameters: previewParameters, 
          target: target) 
関連する問題