首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有可能从其superview的界限中仅解压一个子视图?

是否有可能从其superview的界限中仅解压一个子视图?
EN

Stack Overflow用户
提问于 2019-11-07 20:29:32
回答 2查看 743关注 0票数 1

在这种情况下,我希望将的单个子视图--而不是--剪裁到superview的边界,而superview的其他子视图则被裁剪到它的边界。是否有办法排除某个UIView被剪裁到其superview的界限。类似地,UIView可以指定它确实/不想剪辑到自己边界的子视图吗?

至于代码,我正在使用柯罗达创建类似于Tinder刷卡的UIView。我有一个集合视图,其单元格本质上是Koloda视图。每当卡片开始摇摄时,我都会将其单元格的集合视图的clipsToBounds属性设置为false。这允许在导航栏上方平移单元格,而不需要剪裁。当pan结束时,视图的单元格的集合视图的clipsToBounds属性被设置为true

代码语言:javascript
复制
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**,从超级视图的边界中解压缩所有的子视图,而只能从其超级视图的边界中解压缩一个子视图?**

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-07 22:21:38

这里的简单解决方案是,由于您希望为单个视图设置异常,所以不要移动collectionView单元格。获取一个snapshotView并移动快照;它可以在层次结构中的任何其他位置,而不需要是集合视图的子级。可以使用UIView.convert(来自:)将单元格坐标转换为相应父视图的坐标空间,以正确放置快照。

OP:编辑:

我能够通过采纳Josh的建议并更新我的代码来解决我的问题:

代码语言:javascript
复制
import UIKit
import Koloda

class LikedUserCollectionViewCell: UICollectionViewCell {
    @IBOutlet var swipeView: KolodaView!
    var parentVC: MyViewController!
    //DraggableCardView is a custom Koloda class
    var draggableCardView: DraggableCardView!
}

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) {
        let snapshotView = self.swipeView.snapshotView(afterScreenUpdates: false)!
        snapshotView.accessibilityIdentifier = "snapshotView"
        self.parentVC.view.addSubview(snapshotView)
        snapshotView.frame.origin = self.parentVC.view.convert(swipeView.viewForCard(at: 0)!.frame.origin, from: swipeView)
        draggableCardView = card
        swipeView.viewForCard(at: 0)?.isHidden = true
    }

    func koloda(_ koloda: KolodaView, draggedCardWithPercentage finishPercentage: CGFloat, in direction: SwipeResultDirection) {
        let snapshotView = self.parentVC.view.subviews.first(where: { $0.accessibilityIdentifier == "snapshotView" })!
        snapshotView.frame.origin = self.parentVC.view.convert(draggableCardView.frame.origin, from: swipeView)
    }

    func kolodaPanFinished(_ koloda: KolodaView, card: DraggableCardView) {
        let snapshotView = self.parentVC.view.subviews.first(where: { $0.accessibilityIdentifier == "snapshotView" })!
        draggableCardView = nil
        snapshotView.removeFromSuperview()
        swipeView.viewForCard(at: 0)?.isHidden = false
    }

    func koloda(_ koloda: KolodaView, didSwipeCardAt index: Int, in direction: SwipeResultDirection) {
        //Not relevant to the question
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-11-08 00:20:21

看看您的界面,为什么collectionView位于(导航栏)之上?

在这种情况下,只要collectionView在导航栏后面,就不需要任何剪辑。

要么调用sendSubview(toBack: collectionView),要么将collectionView.layer.zPosition设置为更大的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58756471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档