拖拽在UIImageView上删除多个imageView贴图

时间:2016-09-30 05:19:12

标签: ios iphone swift

我正在拖动来自 UICollectionVIew 的贴纸图片,并将其放在位于 UICollectionView 上方的 UIimageView 上。用户可以在 UIImageView 中移动任何贴纸图像。当我在 UIImageVIew 中选择贴纸图片时,它总是选择我丢弃的最后贴纸。我知道有一些对象不匹配......任何帮助 这是我的代码

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

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

    cell.imgViewColl.tag = indexPath.row

    if(indexPath.row == 1){

        cell.imgViewColl.image = UIImage(named: "applause.png")
    }

    else if(indexPath.row == 2){
     cell.imgViewColl.image = UIImage(named: "baby.png")
    }

    else if(indexPath.row == 3){
     cell.imgViewColl.image = UIImage(named: "basketball.png")
    }

    let panGesture = UIPanGestureRecognizer(target: self, action:#selector(ViewController.handlePan(_:)))
    panGesture.delegate = self
    longGesture.minimumPressDuration = 0.01
    cell.addGestureRecognizer(panGesture)

    return cell    // Create UICollectionViewCell

}

func handlePan(recognizer:UIPanGestureRecognizer) {

    let locationPoint = recognizer.locationInView(self.colleView)

    if recognizer.state == UIGestureRecognizerState.Began {

        if self.colleView.indexPathForItemAtPoint(locationPoint) != nil {

            let indexPathOfMovingCell = self.colleView.indexPathForItemAtPoint(locationPoint)
            let cell = self.colleView.cellForItemAtIndexPath(indexPathOfMovingCell!)
            UIGraphicsBeginImageContext(cell!.bounds.size)
            cell!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
            let cellImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            movingCell = UIImageView(image: cellImage)
            movingCell!.center = recognizer.locationInView(imgView)
            movingCell!.alpha = 1.0
            movingCell?.tag = (cell?.tag)!
            movingCell!.userInteractionEnabled = true
            movingCell!.multipleTouchEnabled = true
            movingCell!.bringSubviewToFront(imgView)
            movingCell!.backgroundColor = UIColor.clearColor()
            imgView.addSubview(movingCell!)
        }

    }


    if recognizer.state == UIGestureRecognizerState.Changed
    {
        if recognizer.state == UIGestureRecognizerState.Changed {
            movingCell!.center = recognizer.locationInView(imgView)
        }
    }
}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    // When a touch starts, get the current location in the view

    let touch = event?.allTouches()?.first
    currentPoint = touch?.locationInView(movingCell!)

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    // Get active location upon move
    let touch = touches.first
    let activePoint = touch?.locationInView(movingCell!)
    //movingCell!.center = touchLocation!

    // Determine new point based on where the touch is now located
    var newPoint = CGPoint(x: movingCell!.center.x + (activePoint!.x - currentPoint!.x), y: movingCell!.center.y + (activePoint!.y - currentPoint!.y))

    //--------------------------------------------------------
    // Make sure we stay within the bounds of the parent view
    //--------------------------------------------------------

    let midPointX: CGFloat = CGRectGetMidX(movingCell!.bounds)
    // If too far right...
    if newPoint.x > movingCell!.superview!.bounds.size.width - midPointX {
        newPoint.x = self.movingCell!.bounds.size.width - midPointX
    }
    else if newPoint.x < midPointX {
        // If too far left...
        newPoint.x = midPointX
    }

    let midPointY: CGFloat = CGRectGetMidY(movingCell!.bounds)
    // If too far down...
    if newPoint.y > movingCell!.superview!.bounds.size.height - midPointY {
        newPoint.y = movingCell!.superview!.bounds.size.height - midPointY
    }
    else if newPoint.y < midPointY {
        // If too far up...
        newPoint.y = midPointY
    }

    // Set new center location
    movingCell!.center = newPoint

}

0 个答案:

没有答案