簡體   English   中英

Tableviewcell中的PanGesture會干擾TableView中的滾動

[英]PanGesture in Tableviewcell interfere Scrolling in TableView

我在TableViewCell中添加PanGesture。
但是在那之后,我無法在TableView中滾動。
我在PanView中滾動時遇到了PanGesture碰撞。
我嘗試了其他解決方案,但仍然無法正常工作。

我的代碼如下。

class ChatlistCell: UITableViewCell{

var leftSwipe = UIPanGestureRecognizer()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    setupForCell()    

}

func setupForCell(){

    //SwipeButton
    //Hide Button
    rightHide.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:150, height:71)
    rightHide.backgroundColor = UIColor(hex:"778295")
    addSubview(rightHide)

    HideLabel.frame = CGRect(x: 19.0, y: 28.0, width: 70, height: 15)
    HideLabel.font = UIFont.systemFont(ofSize:15)
    HideLabel.text = "Hide"
    HideLabel.textColor = UIColor.white
    rightHide.addSubview(HideLabel)

    //Delete Button
    rightDelete.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:75, height:71)
    rightDelete.backgroundColor = UIColor(hex:"ff3600")
    addSubview(rightDelete)

    DeleteLabel.frame = CGRect(x: 25.0, y: 28.0, width: 70, height: 15)
    DeleteLabel.font = UIFont.systemFont(ofSize:15)
    DeleteLabel.text = "Delete"
    DeleteLabel.textColor = UIColor.white
    rightDelete.addSubview(DeleteLabel)


    //Mute Button
    leftMute.frame = CGRect(x:-150, y:0, width:150, height:71)
    leftMute.backgroundColor = UIColor(hex:"5181e2")
    addSubview(leftMute)

    muteImg.frame = CGRect(x:97.5, y:20.5, width:30, height:30)
    muteImg.image = UIImage(named:"chat_list_icon_notice_on")
    leftMute.addSubview(muteImg)

    //Pin Button
    leftPin.frame = CGRect(x:-75, y:0, width:75, height:71)
    leftPin.backgroundColor = UIColor(hex:"82a5e6")
    addSubview(leftPin)

    pinImg.frame = CGRect(x:22.5, y:20.5, width:30, height:30)
    pinImg.image = UIImage(named:"chat_list_icon_pin_off")
    leftPin.addSubview(pinImg)


    leftSwipe = UIPanGestureRecognizer(target: self, action:#selector(self.swipe))
    leftSwipe.delegate = self

    self.mainView.addGestureRecognizer(leftSwipe)
}

func swipe(recognizer: UIPanGestureRecognizer){

    let translation = recognizer.translation(in:self)

    recognizer.setTranslation(CGPoint.zero, in: self)
    print(translation)

    if recognizer.state == UIGestureRecognizerState.began{

    }

    if recognizer.state == UIGestureRecognizerState.ended{
    //    self.mainView.center = CGPoint(x:mainView.center.x + translation.x , y:mainView.center.y)
        if self.mainView.center.x <= UIScreen.main.bounds.width/2 - 76{

            UIView.animate(withDuration: 0.2, delay: 0.0, options: [], animations:{ _ in
                self.mainView.frame = CGRect(x:-152.0, y:0, width:UIScreen.main.bounds.width, height:71)
                self.rightHide.frame = CGRect(x:UIScreen.main.bounds.width - 152, y:0, width:152, height:71)
                self.rightDelete.frame = CGRect(x:UIScreen.main.bounds.width - 76, y:0, width:76, height:71)

            }, completion: nil)

        }else if self.mainView.center.x >= UIScreen.main.bounds.width/2 + 76{

            UIView.animate(withDuration: 0.2, delay: 0.0, options: [], animations:{ _ in
                self.mainView.frame = CGRect(x:152.0, y:0, width:UIScreen.main.bounds.width, height:71)
                self.leftMute.frame = CGRect(x:0, y:0, width:152, height:71)
                self.leftPin.frame = CGRect(x:0, y:0, width:76, height:71)

            }, completion: nil)


        }else{

            UIView.animate(withDuration: 0.2, delay: 0.0, options: [], animations:{ _ in
                self.mainView.frame = CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:71)
                self.rightHide.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:152, height:71)
                self.rightDelete.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:76, height:71)
                self.leftMute.frame = CGRect(x:-152, y:0, width:152, height:71)
                self.leftPin.frame = CGRect(x:-76, y:0, width:76, height:71)

            }, completion: nil)
        }

    }

    if recognizer.state == UIGestureRecognizerState.changed{

        self.mainView.center = CGPoint(x:mainView.center.x + translation.x , y:mainView.center.y)

        //left swipe
        if self.mainView.center.x < UIScreen.main.bounds.width/2{

            if self.mainView.center.x <= UIScreen.main.bounds.width/2 - 152 {
                self.rightHide.center = CGPoint(x:UIScreen.main.bounds.width - 76, y:mainView.center.y)
                self.rightDelete.center = CGPoint(x: UIScreen.main.bounds.width - 38 , y: mainView.center.y)
            }else{
                self.rightHide.center = CGPoint(x:rightHide.center.x + translation.x, y:mainView.center.y)
                self.rightDelete.center = CGPoint(x: rightDelete.center.x + translation.x/2, y: mainView.center.y)
            }
            //right swipe
        }else{

            if self.mainView.center.x >= UIScreen.main.bounds.width/2 + 152 {
                self.leftMute.center = CGPoint(x: 76, y:mainView.center.y)
                self.leftPin.center = CGPoint(x:38 , y: mainView.center.y)
            }else{
                self.leftMute.center = CGPoint(x:leftMute.center.x + translation.x, y:mainView.center.y)
                self.leftPin.center = CGPoint(x: leftPin.center.x + translation.x/2, y: mainView.center.y)
            }

        }

    } else{

    }

}

下面是mainView類,包括TableView。

class ChatlistTabTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate{

@IBOutlet weak var tableView: UITableView!


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let row = allChatroomList[indexPath.row]

    let chatCell = tableView.dequeueReusableCell(withIdentifier: "chatCell") as! ChatListCell

   chatCell.leftSwipe.delegate = self
    return chatCell
}


func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    if gestureRecognizer is UITapGestureRecognizer {
        let location = touch.location(in: tableView)
        return (tableView.indexPathForRow(at: location) == nil)
    }
    return true
}
}

在此處使用HorizPanGestureRecognizer: https//github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch05p203gestureRecognizers/ch18p541gestureRecognizers/HorizVertPanGestureRecognizers.swift

如果平底鍋是垂直的,它將失敗,因此將允許表視圖滾動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM