How can I get around the Rect source corners for Peek and Pop 3D Touch?

In Safari, if you use 3D touch, the sourceRect of the link that touches the touch has rounded corners. When I set the source rectangle to: func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {when previewing the context, I can only set previewingContext.sourceRectthat does not allow me to bypass the corners or set the polygon area. How can i do this?

+4
source share
2 answers

You can indirectly set rounded corners to sourceRect by adding the corner radius to the sourceView level. When you previewingContext.sourceRectframe the sourceView, the area that is in focus will also have rounded corners.

Here is an example that uses UILabel for printing:

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

+6
source

First of all, the sourceView must have a cornerRadius on its layer, indeed, the blur effect will have an angular radius only if the sourceView layer has one. Since sourceView is readonly, it must be set when registration is done using the method registerForPreviewingWithDelegate:sourceView:.

, collectionView:cellForItemAtIndexPath:. , Context , previewingContext, registerForPreviewingWithDelegate:sourceView: :

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

collectionView:didEndDisplayingCell:forItemAtIndexPath: UICollectionViewDelegate :

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

, previewingContext:viewControllerForLocation: UIViewControllerPreviewingDelegate :

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");
-2

Source: https://habr.com/ru/post/1620612/


All Articles