简体   繁体   English

Swift-需要在自定义TableView单元格中单击并拖动UIImageView

[英]Swift — Need to click and drag a UIImageView inside a Custom TableView Cell

I am trying to create a custom TableViewController with a custom UITableViewCell class. 我正在尝试使用自定义UITableViewCell类创建自定义TableViewController。 I need to be able to drag and drop a photo inside the cell to a panel at the bottom of the view. 我需要能够将单元格内的照片拖放到视图底部的面板。 I have tried a few ways to do this, but I am running into a couple of problems and haven't found a complete solution online: 我已经尝试了几种方法来完成此操作,但是遇到了两个问题,但没有在线找到完整的解决方案:

First of all, after dragging and dropping I would like to return the UIImage view to the cell it came from. 首先,在拖放之后,我想将UIImage视图返回到它来自的单元格。

Secondly, I am not able to cast a 'view' to my custom UITableViewClass outside the 'cellForRowAtIndexPath' method (so I will have access to the outlets I have set up.) 其次,我无法在“ cellForRowAtIndexPath”方法之外将自定义UITableViewClass的“视图”转换为(因此我将可以访问已设置的出口。)

Here is my relevant code: 这是我的相关代码:

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)

        }
    }

Thanks for the help... 谢谢您的帮助...

after dragging and dropping I would like to return the UIImage view to the cell it came from. 拖放后,我想将UIImage视图返回到它来自的单元格。

  • Use the transform property to manipulate the position of the foodImageView . 使用transform属性来操纵foodImageView的位置。
  • When the UIGestureRecognizer 's state == UIGestureRecognizerState.Ended , instantiate a new UIImageView and assign the foodImageView image to the new UIImageView 's image property. UIGestureRecognizer的状态== UIGestureRecognizerState.Ended ,实例化一个新的UIImageView并将foodImageView图像分配给新的UIImageViewimage属性。
  • Add the new UIImageView to the desired view. 将新的UIImageView添加到所需的视图。
  • Set the foodImageView 's transform to CGAffineTransformIdentity and animate it. foodImageView的转换设置为CGAffineTransformIdentity并对其进行动画处理。

I'll leave the coding up to you as a learning exercise. 我会将编码留给您作为学习练习。 For reference, here's a good explanation on CGAffineTransform: Demystifying CGAffineTransform . 作为参考,这里是CGAffineTransform的一个很好的解释: 揭开CGAffineTransform的神秘面纱

I am not able to cast a 'view' to my custom UITableViewClass outside the 'cellForRowAtIndexPath' method (so I will have access to the outlets I have set up.) 我无法在“ cellForRowAtIndexPath”方法之外将自定义UITableViewClass的“视图”强制转换为(因此,我将有权访问已设置的出口。)

Instead of adding the UIPanGestureRecognizer to the foodImageView add it to the custom UITableViewCell . 无需将UIPanGestureRecognizer添加到UIPanGestureRecognizerfoodImageView将其添加到自定义UITableViewCell Then, in your wasDragged function, access the UIGestureRecognizer.view , which is the custom UITableViewCell , and then access foodImageView property. 然后,在wasDragged函数中,访问UIGestureRecognizer.view (它是自定义UITableViewCell ,然后访问foodImageView属性。 This will eliminate the need to access the superview property at all. 这将根本不需要访问superview属性。 Accessing the superview property on UI elements in the UIKit framework is never a good idea because Apple can decide to change the structure of the view hierarchy whenever they wish. 在UIKit框架中访问UI元素的superview属性从来都不是一个好主意,因为Apple可以根据需要随时决定更改视图层次结构的结构。 Ultimately, you are essentially guessing at which view you're going to be using and causing your future self headaches. 最终,您实际上是在猜测将要使用哪种视图,并导致将来的自我头痛。

Ex. 防爆。

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)

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM