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