tvOS/Swift 3:如何修复破碎的UICollectionView选择动画?
问题描述:
我已经分类了UICollectionViewCell,以便可以将视差效果应用于整个单元而不仅仅是单元格中的图像。视差效果按我的意愿工作,但它会按下按钮,就像在选定单元格上按下远程按钮时发生的动画。tvOS/Swift 3:如何修复破碎的UICollectionView选择动画?
collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath)被调用,因为它应该是远程按钮,没有任何动画。
我认为我必须做动画,但不知道如何触发它,或者确实如何去做。
这是我UICollectionViewCell:
class CollectionViewCellA: UICollectionViewCell {
@IBOutlet var image: UIImageView!
@IBOutlet var bkgImage: UIImageView!
@IBOutlet var title: UILabel!
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
struct wrapper {
static let s_atvMotionEffect = UIAppleTVMotionEffectGroup()
}
coordinator.addCoordinatedAnimations({
var scale : CGFloat = 0.0
if self.isFocused {
self.addMotionEffect(wrapper.s_atvMotionEffect)
self.layer.shadowOpacity = 0.75;
self.layer.shadowRadius = 32.0;
self.layer.shadowOffset = CGSize(width: 0, height: 16);
scale = 1.2
} else {
self.removeMotionEffect(wrapper.s_atvMotionEffect)
self.layer.shadowOpacity = 0.25;
self.layer.shadowRadius = 4.0;
self.layer.shadowOffset = CGSize(width: 0, height: 0);
scale = 1.0
}
let transform = CGAffineTransform(scaleX: scale, y: scale)
self.layer.setAffineTransform(transform)
},completion: nil)
}
}
这其中UICollectionView所在的UIViewController:
class HomeVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet var freeContentCV: UICollectionView!
@IBOutlet var collectionViewB: UICollectionView!
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var mainImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
freeContentCV.delegate = self
collectionViewB.delegate = self
freeContentCV.dataSource = self
collectionViewB.dataSource = self
}
override func viewDidLayoutSubviews() {
self.scrollView!.contentSize = CGSize(width: 1920, height: 2500)
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
//print(UIScreen.main.focusedView)
}
// Collection View Methods
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.freeContentCV{
return 10
}
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.freeContentCV {
let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CellA", for: indexPath) as! CollectionViewCellA
cellA.bkgImage.image = UIImage(named: "vincent")
cellA.title.text = "TITLE"
return cellA
}
let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CellB", for: indexPath) as! CollectionViewCellA
cellB.image.image = UIImage(named: "vincent")
cellB.title.text = "TITLE"
return cellB
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// What goes here to trigger an animation, if anything?
}
}
我推测didSelectItemAt功能是在那里我有触发按钮的动画,但我不知道如何访问UICollectionViewCell来为其设置动画效果,或者即使这可以通过我的布局来实现。在我的iOS/tvOS理解的边缘,所以我不确定要问的正确问题。
我看了很多来源,还没有发现任何关于这个问题的讨论。
任何想法很好地收到。
答
今天早上对问题进行了彻底解决后,以下是有效的解决方案。
在UICollectionViewCell
class CollectionViewCellA: UICollectionViewCell {
@IBOutlet var image: UIImageView!
@IBOutlet var bkgImage: UIImageView!
@IBOutlet var title: UILabel!
var selectTrans: UIFocusAnimationCoordinator?
var scale : CGFloat = 0.0
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
struct wrapper {
static let s_atvMotionEffect = UIAppleTVMotionEffectGroup()
}
coordinator.addCoordinatedAnimations({
if self.isFocused {
self.addMotionEffect(wrapper.s_atvMotionEffect)
self.layer.shadowOpacity = 0.75;
self.layer.shadowRadius = 10.0;
self.layer.shadowOffset = CGSize(width: 0, height: 5);
self.scale = 1.2
let transform = CGAffineTransform(scaleX: self.scale, y: self.scale)
self.layer.setAffineTransform(transform)
} else {
self.removeMotionEffect(wrapper.s_atvMotionEffect)
self.layer.shadowOpacity = 0.25;
self.layer.shadowRadius = 4.0;
self.layer.shadowOffset = CGSize(width: 0, height: 0);
self.scale = 1.0
let transform = CGAffineTransform(scaleX: self.scale, y: self.scale)
self.layer.setAffineTransform(transform)
}
},completion: nil)
}
func startSelectAnimation() {
scale = 1.1
let transform = CGAffineTransform(scaleX: scale, y: scale)
self.layer.setAffineTransform(transform)
perform(#selector(middleAnimation), with: nil, afterDelay: 0.08)
}
func middleAnimation() {
scale = 0.95
let transform = CGAffineTransform(scaleX: scale, y: scale)
self.layer.setAffineTransform(transform)
perform(#selector(endSelectAnimation), with: nil, afterDelay: 0.1)
}
func endSelectAnimation(){
scale = 1.2
let transform = CGAffineTransform(scaleX: scale, y: scale)
self.layer.setAffineTransform(transform)
}
}
在视图控制器
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// We have to provide animation, so get the cell and do call to animation
let selectedCell = collectionView.cellForItem(at: indexPath) as! CollectionViewCellA
selectedCell.startSelectAnimation()
if collectionView == self.freeContentCV {
performSegue(withIdentifier: "ShowVideoDetail", sender: self)
}
}
动画是有点麻烦,但是当它触发赛格瑞图像跳动是不是所有的明显。