[英]Add UIView to cover the whole UITableViewCell along with accessory view in iOS 10
I have a fairly easy custom cell with standard accessory view. 我有一个带有标准附件视图的相当简单的自定义单元。
At some point I need to perform an animation of covering a whole cell with a colored view which has an image in its center. 在某个时候,我需要执行一个动画,以彩色视图覆盖整个单元,该彩色视图的中心是图像。 I am experiencing strange issues on iPhone 5 with iOS 10.2 while I am doing that. 我在执行iOS 10.2的iPhone 5上遇到奇怪的问题。
This is what I do: 这是我的工作:
Create subclass of UITableViewCell with nib file. 使用nib文件创建UITableViewCell的子类。
Add a UIView coverView
as a subview directly ( self.addSubview(self.coverView)
) to my custom cell (since I need to cover accessory view too, I can't add it to contentView
) in init methods. 在init方法中,将UIView coverView
作为子视图直接( self.addSubview(self.coverView)
)添加到我的自定义单元格中(因为我也需要覆盖辅助视图,因此无法将其添加到contentView
)。 The coverView
is created programmatically with no layout constraints. coverView
是通过编程创建的,没有布局限制。 The UIImageView is added into it programmatically with layout constraints and translatesAutoresizingMaskIntoConstraints
set to false
. 将UIImageView通过布局约束以编程方式添加到其中,并将translatesAutoresizingMaskIntoConstraints
设置为false
。
Add a method: 添加方法:
func animateCState() { func animateCState(){
self.coverView.frame = self.bounds self.coverView.frame.origin.y = -self.frame.size.height self.bringSubview(toFront: self.coverView) UIView.animate(withDuration: 0.3, animations: { self.coverView.frame.origin.y = 0 })
} }
Call animateCState
when user taps on the cell 用户点击单元格时调用animateCState
What I expect: green view with image in center slides from top 我的期望: 绿色视图的顶部幻灯片位于中间
What I get: Apparently transparent view slides from top, and blinks to green almost at the end of animation. 我得到的是: 显然透明的视图从顶部滑动,并在动画结束时几乎闪烁为绿色。 I can see imageView sliding in proper position. 我可以看到imageView在适当的位置滑动。
Question: how to add a view which will cover the whole UITableViewCell (with accessory view) to a cell and be able to animate it? 问题:如何在单元格中添加一个覆盖整个UITableViewCell(带有附件视图)的视图并使其具有动画效果?
Ok. 好。 It seems I have figured it out. 看来我已经知道了。
Apparently UITableView or UITableViewCell does something (set .clear
color as background) to all views added directly to a cell either after prepareForReuse
or at some other point in time. 显然的UITableView或UITableViewCell的做一些事情(设置.clear
颜色作为背景)任后直接加入到小区内所有的观点prepareForReuse
或在某个时间点等。 I had to set the color I want in my animateCState
function right before I do animation. 在执行动画之前,我必须在animateCState
函数中设置所需的颜色。
So my method that works looks like this: 所以我有效的方法如下所示:
func animateCState() {
self.coverView.frame = self.bounds
self.coverView.frame.origin.y = -self.frame.size.height
self.bringSubview(toFront: self.coverView)
self.coverView.backgroundCOlor = .green
UIView.animate(withDuration: 0.3, animations: {
self.coverView.frame.origin.y = 0
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.