2017-03-10 93 views
1

我正在嘗試爲UICollectioView單元創建材質漣漪效應。對於Android來說,有幾個材質設計選項可以這樣做,但對於看起來並非如此的iOS。下面是我使用的原型來填充UICollectioView我的自定義單元格:UICollectionView單元的材質紋波效應

import UIKit 

class PollCell: UICollectionViewCell { 


    @IBOutlet weak var imageView: UIImageView! 

    @IBOutlet weak var pollQuestion: UILabel! 

} 

我在哪裏初始化CollectioViewCell:

override func viewDidLoad() { 
    super.viewDidLoad() 

    ref = FIRDatabase.database().reference() 
    prepareMenuButton() 


    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Register cell classes 

    self.dataSource = self.collectionView?.bind(to: self.ref.child("Polls")) { collectionView, indexPath, snap in 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCell 
     //Here is where I am having issues 
     cell.pulseAnimation 
     /* populate cell */ 
     cell.pollQuestion.text = snap.childSnapshot(forPath: "question").value as! String? 
     let urlPollImage = snap.childSnapshot(forPath: "image_URL").value as! String? 

     cell.imageView.sd_setImage(with: URL(string: urlPollImage!), placeholderImage: UIImage(named: "Fan_Polls_Logo.png")) 
     //Comment 
     return cell 
    } 

這裏是一個設備上的一個細胞的圖像:

enter image description here

回答

1

如果使用Material它有一個CollectionViewCell,它具有內置的脈衝動畫。您可以使用pulseAnimation屬性進行設置。希望這可以幫助。

+0

我已經更新的材料 - 你能指出我應該如何糾正我的代碼?感謝幫助。 – tccpg288

+1

'PollCell'應該繼承Material的'CollectionViewCell',它默認啓用脈衝動畫。這裏是一個示例項目,[CollectionView](https://github.com/CosmicMind/Samples/tree/master/Projects/Programmatic/CollectionView/CollectionView),以幫助您順利完成任務。 – CosmicMind

+0

我得到它的工作,非常感謝!現在我只是難以使單元格點擊.... – tccpg288

4

使用CATransition

func ripple(view:UIView){ 
    let ripple = CATransition() 
    ripple.type = "rippleEffect" 
    ripple.duration = 0.5 
    view.layer.add(ripple, forKey: nil) 
} 

你可以通過任何你想漣漪,它會。示例

self.ripple(view: imageView) 

或者您可以在didselect上傳遞單元格,接觸開始或您用來觸發漣漪的任何內容。

但基於你告訴我你想要一個循環脈衝來覆蓋視圖,所以我試過了。我把一些圖書館考慮在評論中,但這是我對你想要的快速嘗試。

var scaleFactor : CGFloat = 0.6 
    var animationColor : UIColor = UIColor.green 
    var animationDuration : Double = 0.4 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {. 
     super.touchesBegan(touches, with: event) 
     let coverView = UIView(frame: bounds) 
     coverView.autoresizingMask = [.flexibleWidth,.flexibleHeight] 
     coverView.backgroundColor = UIColor.clear 
     self.addSubview(coverView) 

     let touch = touches.first! 
     let point = touch.location(in: self) 

     let ourTouchView = UIView(frame: CGRect(x: point.x - 5, y: point.y - 5, width: 10, height: 10)) 
     print(ourTouchView) 
     print(point) 


     let circleMaskPathInitial = UIBezierPath(ovalIn: ourTouchView.frame) 
     let radius = max((self.bounds.width * scaleFactor) , (self.bounds.height * scaleFactor)) 
     let circleMaskPathFinal = UIBezierPath(ovalIn: ourTouchView.frame.insetBy(dx: -radius, dy: -radius)) 


     let rippleLayer = CAShapeLayer() 
     rippleLayer.opacity = 0.4 
     rippleLayer.fillColor = animationColor.cgColor 
     rippleLayer.path = circleMaskPathFinal.cgPath 
     coverView.layer.addSublayer(rippleLayer) 

     //fade up 
     let fadeUp = CABasicAnimation(keyPath: "opacity") 
     fadeUp.beginTime = CACurrentMediaTime() 
     fadeUp.duration = animationDuration * 0.6 
     fadeUp.toValue = 0.6 
     fadeUp.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) 
     fadeUp.fillMode = kCAFillModeForwards 
     fadeUp.isRemovedOnCompletion = false 
     rippleLayer.add(fadeUp, forKey: nil) 

     //fade down 
     let fade = CABasicAnimation(keyPath: "opacity") 
     fade.beginTime = CACurrentMediaTime() + animationDuration * 0.60 
     fade.duration = animationDuration * 0.40 
     fade.toValue = 0 
     fade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) 
     fade.fillMode = kCAFillModeForwards 
     fade.isRemovedOnCompletion = false 
     rippleLayer.add(fade, forKey: nil) 

     //change path 
     CATransaction.begin() 
     let maskLayerAnimation = CABasicAnimation(keyPath: "path") 
     maskLayerAnimation.fromValue = circleMaskPathInitial.cgPath 
     maskLayerAnimation.toValue = circleMaskPathFinal.cgPath 
     maskLayerAnimation.beginTime = CACurrentMediaTime() 
     maskLayerAnimation.duration = animationDuration 
     maskLayerAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) 
     CATransaction.setCompletionBlock({ 
      coverView.removeFromSuperview() 
     }) 
     rippleLayer.add(maskLayerAnimation, forKey: "path") 
     CATransaction.commit() 
    } 

我可能不會執行此觸摸開始,而是使用輕擊手勢,但你可以做到這一點。

+0

我將如何設置與單元格的黑色相匹配的顏色?我在想一個更淺的灰色,並且你在使用第三方庫嗎? – tccpg288

+0

沒有第三方庫。這只是讓它看起來像一滴水。這只是老派。你想在該位置觸摸事件的顏色爆發嗎? – agibson007

+0

是的,我應該更具體。我甚至沒有連接觸摸但 – tccpg288