5

在 Safari 中,如果您使用 3D 触摸,则被触摸的链接的 sourceRect 具有圆角。当我将源矩形设置为:func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {在previewingContext上时,我只能设置previewingContext.sourceRect不允许我圆角的地方,或者设置一个多角区域。我怎样才能做到这一点?

4

2 回答 2

5

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

这是一个使用可按下 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)
    }
}

在此处输入图像描述

于 2015-12-18T14:41:22.047 回答
-2

首先 sourceView 应该在它的 layer 上有一个cornerRadius,实际上模糊效果只有 sourceView 的 layer 有一个圆角半径时才会有一个圆角半径。因为 sourceView 是只读的,所以在注册的时候需要设置registerForPreviewingWithDelegate:sourceView:

例如,在其单元格上具有圆角半径的集合视图中,可以在collectionView:cellForItemAtIndexPath:. registerForPreviewingWithDelegate:sourceView:为了安全起见,由于 previewingContext 稍后将被检查,我保留了一个指向单元格本身返回的 previewingContext 的弱指针:

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    id previewingContext = [self registerForPreviewingWithDelegate:self sourceView:cell];
    cell.weakPreviewingContext = previewingContext;
}

然后在UICollectionViewDelegatecollectionView:didEndDisplayingCell:forItemAtIndexPath:协议的方法中,我这样取消注册:

if (collectionView == self.collectionView) {
    if ([cell isKindOfClass:UserInHomeCollectionCell.class]) {
        [self unregisterForPreviewingWithContext:((UserInHomeCollectionCell*)cell).weakPreviewingContext];
    }
}

最后在UIViewControllerPreviewingDelegatepreviewingContext: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");
于 2016-05-06T15:55:26.093 回答