0
我正在開發一個應用程序,用戶通過「卡片」進行滑動,這些卡片基本上是用API中的數據填充的UIViews。在使用手勢識別器添加子視圖時遇到用戶界面故障
當應用第一次打開時,卡片被創建並作爲子視圖添加到超級視圖。每次用戶刷卡時,該卡都將從超級視圖中移除。
當用戶刷卡時,會在後臺創建並緩存更多卡片。
當用戶在最後一張卡片上時,我將緩存的卡片添加到當前正在顯示的卡片背後的超級視圖。
問題是,當卡被添加到超級視圖時,我的手勢識別器停止工作/毛刺(用戶不能滑動)。幾秒鐘後,它再次開始工作,用戶可以滑動。
如何阻止識別器發生故障?任何洞察力將不勝感激。
謝謝!
下面是一個創建/添加卡 //創建卡 FUNC createCards(新華美通:布爾)代碼{
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
@dfd添加了代碼 –