2017-10-04 38 views
0

我有一個Instagram贊飼料 飼料有很多職位。每篇文章都有類似按鈕。如何在swift 3中每次在我的Feed中喜歡某個帖子時執行動畫?

每當用戶點擊喜歡 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用這種方法得到正確的所需的位置。

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

現在我FeedController(這是UIcollectionViewController)

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 

}

現在,當我點擊像按鈕,從第一篇文章我的動畫作品,但是當我向下滾動另一個帖子並點擊按鈕,動畫仍然有效,但無法看到,因爲它發生在頂端。每改變一次按鈕,我都會改變座標,但CGPoint起始點的路徑從進給起點開始計算。我應該做什麼使它正常工作?

回答

0

首先,我不明白爲什麼您將位置傳遞給FeedController,而您可以在UICollectionViewCell的類中執行所有動畫。該動畫是否發生在不屬於Post Cell(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 
         } 
        }) 
} 
相關問題