2017-10-04 5 views
0

Instagram Like 피드가 있습니다. 피드에 여러 게시물이 있습니다. 각 게시물에는 같은 버튼이 있습니다.빠른 피드 3에서 내 피드에 게시물이 좋아질 때마다 애니메이션을 어떻게 수행합니까?

매번 사용자는 내가 점점이 방법을 사용하여 올바른 필요한 위치를 얻을 delegate.I을 통해 사용자 탭의 위치를 ​​통과하고

let likeImageView: UIButton = { 
      let button = UIButton(type: .custom) 
      button.setImage(#imageLiteral(resourceName: "like_unselected"), for: .normal) 
      let tap = UITapGestureRecognizer(target: self, action: #selector(loveButtonTapped)) 
      tap.numberOfTapsRequired = 1 
      button.addGestureRecognizer(tap) 

      return button 
     }() 

MyUiView 식으로 도청. 나는 아래로 스크롤 할 때 내가 애니메이션 작품 첫 번째 게시물에서 버튼 식으로 탭하지만 (UIcollectionViewController입니다) 내 FeedController 이제

func likeButtonTapped(loc: CGPoint, for cell: PostUi){ 
      print("like button tapped") 
      var touchLocation: CGPoint = loc 
      print(touchLocation) 
     ... 
     ... //like implementation and checks 
     ... 

var i = Int(location.x) 
var j = Int(location.y) 

let path = UIBezierPath() 

path.move(to: CGPoint(x:i, y:j)) 
//more code for animation 

}

, 지금

@objc func loveButtonTapped(sender: UITapGestureRecognizer){ 
     guard var location : CGPoint = sender.view?.superview?.frame.origin else { return } 
     var loc = convert(location, to: nil) 
     self.delegate?.onloveButtonTapped(loc: loc, for: self) 
    } 

다른 게시물과 같은 버튼을 살짝 두드려서, 애니메이션은 여전히 ​​작동하지만 상단에서 일어나기 때문에 볼 수 없습니다. 내가 좋아하는 모든 버튼에 대한 좌표가 있지만 CGPoint 시작 경로는 피드 시작 지점에서부터 계산됩니다. 어떻게하면 제대로 작동합니까?

답변

0

먼저 UICollectionViewCell의 클래스에서 모든 애니메이션을 수행 할 수 있지만 위치를 FeedController로 전달하는 이유는 무엇입니까? 애니메이션이 포스트 셀 (UICollectionViewCell) 내부에없는 다른 UIView에서 발생합니까?

@IBAction func likeBtnTapped(_ sender: UIButton) { 
    if post != nil { 
     putLike(postId: post!.id, userId: user.id, isLiked: post!.liked) 
    } 
} 

// Network Requests 
func putLike(postId: Int, userId: Int, isLiked: Bool) { 
    let httpService = HTTPService() 
    httpService.putLikeRequest(postId: postId, userId: userId, isLiked: isLiked, onSuccess: { (liked, likeText) in 
     self.post?.liked   = liked 
     self.post?.likesText  = likeText 
     if self.post != nil { 
      self.likeCountLbl.text = self.post!.likesText 
      self.delegate?.replaceLikedPostInArray(post: self.post!) 
      self.likeButtonScaleAnimation(liked: liked) 
     } 
    }) { (error) in 
     print("putLikeRequest error : \(error)") 
    } 
} 

func likeButtonScaleAnimation(liked: Bool) { 
    UIView.animate(withDuration: 0.4, 
        animations: { 
        self.likeBtn.transform  = liked ? CGAffineTransform(scaleX: 1.4, y: 1.4) : CGAffineTransform(scaleX: 0.6, y: 0.6) 
        self.likeBtn.tintColor  = liked ? UIColor.blue        : UIColor.darkGray 
        }, 
        completion: { _ in 
        UIView.animate(withDuration: 0.4) { 
         self.likeBtn.transform = CGAffineTransform.identity 
         } 
        }) 
}