在使用手势识别器添加子视图时遇到 UI 故障问题
Posted
技术标签:
【中文标题】在使用手势识别器添加子视图时遇到 UI 故障问题【英文标题】:Having trouble with UI glitch while adding subviews with Gesture Recognizers 【发布时间】:2016-12-03 02:41:40 【问题描述】:我正在开发一个应用程序,用户可以在其中滑动“卡片”,这些卡片基本上是填充了来自 API 的数据的 UIView。
当应用首次打开时,会创建卡片并将其作为子视图添加到父视图。每次用户刷卡时,卡片都会从 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]
【问题讨论】:
因为事情(但最终)以您想要的方式工作,这可能意味着您已经正确编码了“基线”。如果您可以发布一些代码供我们查看,将会有所帮助。 @dfd 添加了代码 【参考方案1】:想通了。一次加载太多 UIView。更好更流畅的方法是不断回收两个 UIViews
【讨论】:
以上是关于在使用手势识别器添加子视图时遇到 UI 故障问题的主要内容,如果未能解决你的问题,请参考以下文章