2016-12-03 3 views
0

기본적으로 사용자가 API의 데이터로 채워진 UIView 인 '카드'를 스 와이프하는 앱을 개발하고 있습니다.제스처 인식자를 사용하여 하위보기를 추가하는 동안 UI 결함에 문제가 발생했습니다.

앱이 처음 열리면 카드가 만들어져 수퍼 뷰에 하위보기로 추가됩니다. 사용자가 스 와이프 할 때마다 수퍼 뷰에서 카드가 제거됩니다.

사용자가 스 와이프하는 동안 더 많은 카드가 만들어지고 백그라운드에서 캐시됩니다.

사용자가 마지막 카드에있을 때 캐시 된 카드를 현재 표시된 카드 뒤의 수퍼바이저에 추가합니다.

카드가 수퍼 뷰에 추가되는 동안 내 제스처 인식기가 작동/글리치를 중지합니다 (사용자가 스 와이프 할 수 없음). 몇 초 후에 다시 작동하기 시작하고 사용자가 스 와이프 할 수 있습니다.

어떻게 인식기가 글리치를 멈추게합니까? 모든 통찰력은 인정 될 것이다.

감사합니다. 을 heres 인식기를 추가 코드/캐시

// 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] 
} 
을 추가 {

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") 
     } 
    } 

} 

: 여기

는 카드를 FUNC의 createCards (BOOL 연합 뉴스)를 만들기 // /이 카드 를 추가 생성하는 코드입니다
+0

상황이 (결국) 원하는 방식으로 작동하기 때문에 아마 "기준선"을 코딩했음을 의미 할 것입니다. 우리가 볼 수있는 코드를 게시 할 수 있다면 도움이 될 것입니다. – dfd

+0

@dfd가 코드를 추가했습니다. –

답변

0

알아 냈어. 한 번에 너무 많은 UIView를로드하고 있습니다. 훨씬 더 매끄러운 접근자는 두 개의 UIView를 지속적으로 재활용하는 것입니다