gpt4 book ai didi

ios - 使用平移手势从 collectionView 中拖动新创建的 subview

转载 作者:行者123 更新时间:2023-11-30 13:42:01 25 4
gpt4 key购买 nike

我有一个 ViewController,下面有一个基本的 Collection View 。我现在可以单击 Collection View 中的单元格并添加所选图像的 subview 。我坚持允许用户按下单元格并将新的 subview 拖动到主视图上,而无需用户抬起手指并重新选择,从而激活平移手势。

如何以编程方式为新创建的 subview 启动平移手势,使用户看起来像是从 collectionView 中拖动副本?

func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
stickerImage = imageArray[indexPath.item]!

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
dispatch_async(dispatch_get_main_queue()) {
// update UI on the main thread
var newImageView = UIImageView(image: self.stickerImage)

newImageView.userInteractionEnabled = true
newImageView.multipleTouchEnabled = true
newImageView.exclusiveTouch = true
newImageView.contentMode = .ScaleAspectFit

let panGesture = UIPanGestureRecognizer(target: self, action:Selector("handlePan:"))
panGesture.delegate = self
newImageView.addGestureRecognizer(panGesture)


newImageView.frame = self.view.bounds
newImageView.frame.size.width = 150
newImageView.frame.size.height = 150

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell

newImageView.center.y = self.view.frame.height - collectionView.frame.height / 2
newImageView.center.x = cell.center.x

self.view.addSubview(newImageView)
self.view.bringSubviewToFront(newImageView)
}
}

}

func handlePan(recognizer:UIPanGestureRecognizer) {

self.view.bringSubviewToFront(recognizer.view!)
let translation = recognizer.translationInView(recognizer.view)
if let view = recognizer.view {
view.transform = CGAffineTransformTranslate(view.transform, translation.x, translation.y)
}
recognizer.setTranslation(CGPointZero, inView: recognizer.view)
}

最佳答案

您是否尝试过重写触摸方法,例如 touchbegin 和 touchend。重写这些方法,您将不必关心平移手势,只需检查识别触摸的 View 。

关于ios - 使用平移手势从 collectionView 中拖动新创建的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35445430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com