简体   繁体   English

添加UIView以覆盖整个UITableViewCell以及iOS 10中的附件视图

[英]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: 这是我的工作:

  1. Create subclass of UITableViewCell with nib file. 使用nib文件创建UITableViewCell的子类。

  2. 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

  3. 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 }) 

    } }

  4. 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.

相关问题 添加按钮到UITableViewCell的附件视图 - Add button to UITableViewCell's Accessory View 如何在调整应用程序中将UIView添加到整个iOS的最前面的视图? - How to add an UIView to the front most view of the whole iOS in the tweak application? iOS UITableViewCell配件在左侧? - iOS UITableViewCell accessory on the left side? 将UIImageView加载到UITableViewCell附件视图中 - Loading UIImageView into UITableViewCell Accessory View iOS UITableViewCell选择关于单元格问题的背景视图封面图像 - iOS UITableViewCell selected background view cover image on cell issue 在编辑模式下将xib添加为视图以覆盖自定义UITableViewCell - Add xib as a view to cover a custom UITableViewCell in editing mode UITableViewCell contentLayer backgroundColor不会覆盖附件的背景 - UITableViewCell contentLayer backgroundColor won't cover accessory's background ios / xcode:如何将附件视图添加到自定义单元格 - ios/xcode:How to Add Accessory View to Custom Cell iOS:使用nib将UITableViewCell子类化为UIView(不是视图控制器)子类 - iOS: UITableViewCell subclassing with nib, into UIView (not view controller) subclass UITableViewCell-在初始化程序中设置背景视图和附件 - UITableViewCell - setting background view and accessory in the initializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM