我正在开发一个应用程序,用户可以在其中滑动“卡”,这些卡基本上是用API数据填充的UIViews。

首次打开应用程序时,将创建卡片并将其作为子视图添加到超级视图。每次用户滑动时,都会从超级视图中移除该卡片。

当用户刷卡时,将创建更多卡并将其缓存在后台。

当用户位于最后一张卡片上时,我将缓存的卡片添加到当前显示的卡片后面的Superview中。

问题是在将卡片添加到超级视图时,我的手势识别器停止工作/出现故障(用户无法滑动)。几秒钟后,它再次开始工作,用户可以滑动。

如何阻止识别器出现故障?任何见识将不胜感激。

谢谢!

这是创建/添加卡的代码
//创建卡片
    func createCards(firstCall:Bool){

    APICall.begin () { (info) in

        DispatchQueue.main.async {
            for i in info {

                let frontView = frontView()
                let backView = backView()

                // Set up front and back views
                self.viewSetup(view: frontView)
                self.viewSetup(view: backView)

                // Set up card view
                let view = cardView(frame: frame)
                view.addSubview(backView)
                view.addSubview(frontView)
                self.viewSetup(view: view)
                view.isHidden = true

                // function that add recognizer
                self.dragging(view: view)

                // cacheCard
                if !firstCall{
                    self.cache.add(cardView: view)
                // add to view
                }else{
                    self.view.addSubview(view)
                }

            }

            // Reveal current card
            if firstCall {
                self.view.subviews.last?.isHidden = false
            }

            print("DONE")
        }
    }

}


这是添加识别器/添加缓存的代码

// dragging gesture helper
func draggig(card: view){
    let gesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.wasDragged(_:)))
    view.addGestureRecognizer(gesture)
    view.isUserInteractionEnabled = true

}

// dragged gestrues
func wasDragged(_ gesture: UIPanGestureRecognizer){

    // Only allow dragging when front is showing
    if(showingFront){

        // Dragging code
        let helper = draggingBeganHelper(gesture: gesture)
        let card:UIView = helper[0] as! UIView
        var rotation:CGAffineTransform = helper[1] as! CGAffineTransform
        var stretch:CGAffineTransform = helper.last as! CGAffineTransform

        if gesture.state == UIGestureRecognizerState.ended {

            rotation = CGAffineTransform(rotationAngle: 0)
            stretch = rotation.scaledBy(x: 1, y: 1)

            //Remove card from superview
            card.removeFromSuperview()

            // Store cards in cache
            if(self.view.subviews.count == lastCardIndex + cardOffset){

                createCards(false)

            }

            // When at the second to last card
            else if self.view.subviews.count == lastCardIndex+1{
                if cardCacheArray.isEmpty {
                    createCards(true)
                }
                else{
                    DispatchQueue.main.async {
                        for i in cardCacheArray{
                            self.view.insertSubview(i, belowSubview: self.view.subviews[lastCardIndex])
                        }
                        self.cache.empty()
                    }
                }
            }
        }
    }

}

func draggingBeganHelper(gesture: UIPanGestureRecognizer) -> [AnyObject]{
    // Card dragging gesture setup
    let translation = gesture.translation(in: self.view)
    let card = gesture.view
    card?.center = CGPoint(x: (card?.center.x)! + translation.x, y: (card?.center.y)! + translation.y)
    let xFromCenter = (card?.center.x)! - self.view.bounds.width/2
    let scale = min(100/abs(xFromCenter),1)
    let rotation = CGAffineTransform(rotationAngle:  xFromCenter/200)
    let stretch = rotation.scaledBy(x: scale, y: scale)
    card?.transform = stretch

    return [card!,rotation as AnyObject,stretch as AnyObject]
}

最佳答案

弄清楚了。一次加载太多的UIView。更好,更流畅的方法是不断回收两个UIViews

关于ios - 使用手势识别器添加 subview 时遇到UI故障,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40943712/

10-17 03:16