如何为Peek和Pop 3D Touch圆角源矩形?

5

在Safari中,如果使用3D Touch,被触摸链接的sourceRect具有圆角。当我在previewingContext上设置source rect时:func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 我只能设置previewingContext.sourceRect,这不允许我设置圆角或者多角区域。我该怎么做呢?

2个回答

5

您可以通过将圆角半径添加到源视图的图层来间接设置源矩形的圆角。当您将previewingContext.sourceRect设置为源视图的边界时,保持焦点的区域也会具有圆角。

这里有一个使用可按压的UILabel的示例:

class ViewController: UIViewController {

    var previewingContext: UIViewControllerPreviewing?
    let label = UILabel(frame: CGRectMake(150, 250, 100, 50))

    override func viewDidLoad() {
        super.viewDidLoad()

        let background = UIImageView(frame: view.bounds)
        background.image = UIImage(named: "image.jpg")
        view.addSubview(background)

        label.backgroundColor = UIColor.whiteColor()
        label.text = "Press me!"
        label.textAlignment = .Center
        label.layer.cornerRadius = 20
        label.clipsToBounds = true
        label.userInteractionEnabled = true
        view.addSubview(label)

        previewingContext = registerForPreviewingWithDelegate(self, sourceView: label)
    }
}

extension ViewController: UIViewControllerPreviewingDelegate {

    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        previewingContext.sourceRect = label.bounds

        return UIViewController()
    }

    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
        showViewController(viewControllerToCommit, sender: self)
    }
}

enter image description here


2
嗨@joem,如何在UITableView中实现这个功能呢?我可能不应该为每个UITableViewCell创建一个id<UIViewControllerPreviewing>,但是我想展示一些圆角视图(例如,像Apple在消息应用程序中所做的那样;3D Touch头像)。 - brechtb

-2
首先,sourceView 应该在其层上具有 cornerRadius,因为只有当 sourceView 的层具有 cornerRadius 时,模糊效果才会具有圆角半径。由于 sourceView 是只读的,因此应在使用方法 registerForPreviewingWithDelegate:sourceView: 进行注册时设置它。
例如,在具有单元格圆角半径的集合视图中,可以在 collectionView:cellForItemAtIndexPath: 中进行注册。为了安全起见,并且稍后将检查 previewingContext,我在单元格本身中保留了对 registerForPreviewingWithDelegate:sourceView: 返回的 previewingContext 的弱引用:
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    id previewingContext = [self registerForPreviewingWithDelegate:self sourceView:cell];
    cell.weakPreviewingContext = previewingContext;
}

然后在UICollectionViewDelegate协议的方法collectionView:didEndDisplayingCell:forItemAtIndexPath:中,我进行了注销操作:
if (collectionView == self.collectionView) {
    if ([cell isKindOfClass:UserInHomeCollectionCell.class]) {
        [self unregisterForPreviewingWithContext:((UserInHomeCollectionCell*)cell).weakPreviewingContext];
    }
}

最后,在UIViewControllerPreviewingDelegate协议的方法previewingContext:viewControllerForLocation:中,我进行了以下安全检查:

UserInHomeCollectionCell *cell = (UserInHomeCollectionCell*)[(UIViewController*)previewingContext view];
NSAssert([cell isKindOfClass:UserInHomeCollectionCell.class], @"***** INTERNAL ERROR: Invalid class for retrieved cell %@", cell);
NSAssert([previewingContext isEqual:((UserInHomeCollectionCell*)cell).weakPreviewingContext], @"***** INTERNAL ERROR: Invalid Previewing Context");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接