2015-12-17 21 views
4

在Safari中,如果你使用的3D觸摸,即觸摸的鏈接與sourceRect有圓角。當我設置在源rect:func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {上previewingContext,我只能設置previewingContext.sourceRect這並不讓我圓邊角,或者設置多角球區。我怎樣才能做到這一點?我怎樣才能將sourceRect的角落變成Peek和Pop 3D Touch?

回答

6

可以間接地通過增加一個圓角半徑爲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) 
    } 
} 

enter image description here

+2

嗨@joem,怎麼會去實現這個了'UITableView',在這個意義上,我大概不應該讓一個'ID '每一個'UITableViewCell',但想顯示一些圓角的意見,(例如。像Apple在消息應用程序中所做的那樣; 3D觸摸頭像) – brechtb

-2

首先在sourceView應該有一個cornerRadius的層上,確實模糊效果將有一個圓角半徑只有sourceView的圖層有一個。由於sourceView是隻讀的,因此在使用方法registerForPreviewingWithDelegate:sourceView:進行註冊時應該設置它。

如結合其細胞角半徑的集合視圖爲例,登記可以在collectionView:cellForItemAtIndexPath:製成。而對於安全性,並作爲previewingContext將在稍後檢查,我保持一個弱指針previewingContext在細胞本身返回由registerForPreviewingWithDelegate:sourceView:

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