2017-10-04 1 views
0

私はInstagram Likeフィードを持っています フィードには多数の投稿があります。各投稿には同じようなボタンがあります。すばやく3のフィードで投稿が好きになるたびにアニメーションを実行するにはどうすればよいですか?

毎回ユーザーが MyUiView

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

ようにタップ私はdelegate.Iを介してユーザのタップの位置を取得して渡しています。このメソッドを使用して正しい必要な場所を取得します。私は下にスクロールするとき、私は私のアニメーション作品の最初の記事からボタンのようにタップしますが(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 
         } 
        }) 
} 
関連する問題