【发布时间】:2020-03-04 11:46:50
【问题描述】:
我有一种情况,我希望单个子视图不被裁剪到父视图的边界,而父视图的其余子视图被裁剪到其边界。有没有办法将某个UIView 排除在其超级视图的范围内。同样,UIView 可以指定它想要/不想裁剪到自己的边界的子视图吗?
就代码而言,我使用Koloda 创建类似于Tinder 的刷卡的UIViews。我有一个集合视图,其单元格本质上是 Koloda 视图。每当卡片开始平移时,我将其单元格的集合视图的clipsToBounds 属性设置为false。这允许单元格在导航栏上方平移而不会被剪裁。当平移结束时,视图单元格的集合视图的clipsToBounds 属性设置回true。
import UIKit
import Koloda
class LikedUserCollectionViewCell: UICollectionViewCell {
@IBOutlet var swipeView: KolodaView!
var parentVC: MyViewController!
}
extension LikedUserCollectionViewCell: KolodaViewDelegate, KolodaViewDataSource {
func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
//return a KolodaView, this code isn't important to the issue
return KolodaView()
}
func kolodaNumberOfCards(_ koloda: KolodaView) -> Int {
return 1
}
func kolodaPanBegan(_ koloda: KolodaView, card: DraggableCardView) {
self.parentVC.collectionView.bringSubviewToFront(self)
//Here, instead of unclipping all of the collection view's subviews, I would like to only unclip the one containing the subview that is being panned
self.parentVC.collectionView.clipsToBounds = false
}
func kolodaPanFinished(_ koloda: KolodaView, card: DraggableCardView) {
//Once the pan is finished, all of the collection view's subviews can be clipped again
self.parentVC.collectionView.clipsToBounds = true
}
func koloda(_ koloda: KolodaView, didSwipeCardAt index: Int, in direction: SwipeResultDirection) {
//Not relevant to the question
}
}
问题是,当有部分单元格从集合视图中滚动出来,但还没有从集合视图中移除,因为它们还没有完全滚出集合视图,它们现在变得可见为集合视图的clipsToBounds现在设置为true。这就是为什么我希望能够指定集合视图的哪些子视图被剪裁到其边界。这种行为可以在下面的 gif 中看到:
所以,总而言之,有没有办法,而不是通过 superview.clipsToBounds = false 从其父视图的边界取消剪辑所有子视图,我只能从其父视图的边界取消剪辑单个子视图?
【问题讨论】:
标签: ios swift uiview uikit clipstobounds