【问题标题】:Trying to apply pan gesture to UITableViewCell but failed, why?尝试对 UITableViewCell 应用平移手势但失败,为什么?
【发布时间】:2016-05-20 05:36:24
【问题描述】:

因此,我学会了如何在视图中的图片上做出平移手势,并四处移动它。然后我尝试用 TableViewCell 做同样的事情:用平移手势水平移动它,它只是没有反应:

所以我创建了TableViewController.swift,设置了常用的东西以确保它正常运行并显示带有动态表格单元格的数据列表。 代码:

var todoItems = [TodoItem]()
override func viewDidLoad() {
    super.viewDidLoad()
    createSomeData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return todoItems.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(constant.cellIdentifier, forIndexPath: indexPath) as! TodoTableViewCell
    cell.todoItem = todoItems[indexPath.row]

    return cell
}

然后我从调色板中取出Pan Gesture Reconizer,将其放在tableviewcell 上,确保它连接到连接检查器中,然后,ctrl + 将Pan Gesture Reconizer 拖到TableViewController.swift 中:

@IBAction func handlePan(gesture: UIPanGestureRecognizer) {
    let transition = gesture.translationInView(self.view)
    switch gesture.state{
    case .Changed:
        if let view = gesture.view {
            view.center = CGPoint(x: view.center.x + transition.x, y: view.center.y)
        }
        gesture.setTranslation(CGPointZero, inView: self.view)

    default:break
    }

}

它不起作用。为什么?

【问题讨论】:

    标签: ios swift uitableview gesture uipangesturerecognizer


    【解决方案1】:

    您可以尝试使用以下代码以编程方式添加PanGesture

    var panRecognizer: UIPanGestureRecognizer!
    @IBOutlet weak var tableView: UITableView?
    
    override func viewDidLoad() {
    super.viewDidLoad()
    
        self.panRecognizer = tableView?.panGestureRecognizer;
        self.panRecognizer .addTarget(self, action: "pan:")
        tableView?.addGestureRecognizer(self.panRecognizer)
    
    }
    
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    func pan(rec:UIPanGestureRecognizer){
        println("PanGesture Activated")
    }
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多