Swift - 需要在Custom TableView Cell中单击并拖动UIImageView

时间:2015-12-31 05:43:16

标签: ios iphone swift uitableview uiimageview

我正在尝试使用自定义UITableViewCell类创建自定义TableViewController。我需要能够将单元格内的照片拖放到视图底部的面板中。我尝试了几种方法来做到这一点,但我遇到了一些问题,并且没有找到完整的在线解决方案:

首先,在拖放之后,我想将UIImage视图返回到它来自的单元格。

其次,我无法投出一个观点'到我的自定义UITableViewClass之外的' cellForRowAtIndexPath'方法(所以我可以访问我设置的插座。)

以下是我的相关代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let thisCell:MealPlanTableViewCell = tableView.dequeueReusableCellWithIdentifier("mealPlanCell", forIndexPath: indexPath) as! MealPlanTableViewCell

    let meal = mealPlanElementArray[indexPath.row]

    thisCell.postTextLabel.text = meal.2
    thisCell.foodImageView.image = meal.4

    let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
    thisCell.foodImageView.addGestureRecognizer(gesture)
    thisCell.foodImageView.userInteractionEnabled = true

    return thisCell
}

func wasDragged(gesture: UIPanGestureRecognizer) {
    if calendarIsUp == calendarIsUp {
        let meal = gesture.view! as! UIImageView
        let sview = gesture.view!.superview!.superview!
        // CAN'T DO THE FOLLOWING -- CAN'T CAST TO CUSTOM CELL
        let mptvc:MealPlanTableViewCell = gesture.view!.superview!.superview! as! MealPlanTableViewCell

        if beginningDrag == true
        {


            beginningDrag = false

            // Move
            meal.superview?.superview?.superview?.superview?.superview?.superview?.superview?.addSubview(meal)

            beforeDragX = (meal.center.x)
            beforeDragY = (meal.bounds.maxY)

        }
        let translation = gesture.translationInView(self.view)

        meal.center = CGPoint(x: beforeDragX + translation.x, y: beforeDragY + translation.y)


        if gesture.state == UIGestureRecognizerState.Ended {

            beginningDrag = true
            // NEED TO ADD BACK TO custom UITableViewCell class.
            sview.addSubview(meal)
            meal.center = CGPoint(x: beforeDragX, y: beforeDragY)

        }
    }

感谢您的帮助...

1 个答案:

答案 0 :(得分:1)

  拖放后我想将UIImage视图返回到   它来自的细胞。

  • 使用transform属性来操纵位置 foodImageView
  • UIGestureRecognizer状态== UIGestureRecognizerState.Ended时,实例化新UIImageView并将foodImageView图片分配给新UIImageView& #39; s image属性。
  • 将新UIImageView添加到所需的视图。
  • foodImageView的转换设为CGAffineTransformIdentity并为其设置动画。

我会把编码留给你作为学习练习。作为参考,这里有关于CGAffineTransform的一个很好的解释:Demystifying CGAffineTransform

  

我无法投出视图'到我的自定义UITableViewClass外面   ' cellForRowAtIndexPath'方法(所以我将有权访问   我设立的网点。)

而不是将UIPanGestureRecognizer添加到foodImageView,而不是将其添加到自定义UITableViewCell。然后,在wasDragged函数中,访问UIGestureRecognizer.view,即自定义UITableViewCell,然后访问foodImageView属性。这将消除访问superview属性的需要。访问UIKit框架中UI元素上的superview属性绝不是一个好主意,因为Apple可以随时决定更改视图层次结构。最终,你实际上在猜测你将使用哪种观点并导致你未来的自我头痛。

<强>实施例

func wasDragged(gesture: UIPanGestureRecognizer)
{
    if calendarIsUp == calendarIsUp
    {
        let cell = gesture.view! as! MealPlanTableViewCell

        if beginningDrag
        {
            beginningDrag = false

            beforeDragX = cell.foodImageView.center.x
            beforeDragY = cell.foodImageView.bounds.maxY

            // Move
            /*
                I have no idea what view this is so I don't know what to change it to.
            */
            cell.foodImageView.superview?.superview?.superview?.superview?.superview?.superview?.superview?.addSubview(cell.foodImageView)
        }

        let translation = gesture.translationInView(self.view)
        cell.foodImageView.center = CGPoint(x: beforeDragX + translation.x, y: beforeDragY + translation.y)

        if gesture.state == UIGestureRecognizerState.Ended
        {
            beginningDrag = true
            // NEED TO ADD BACK TO custom UITableViewCell class.
            cell.contentView.addSubview(cell.foodImageView)
            cell.foodImageView.center = CGPoint(x: beforeDragX, y: beforeDragY)

        }
    }
}