2017-03-10 92 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