我想制作一个类似 Facebook 的 iOS 应用程序,当用户点击它时,它会从时间轴全屏显示图片。
更新:
我正在使用 UICollectionView 在单元格中显示图像,
所以我似乎应该使用 collectionView:didSelectItemAtIndexPath:
方法?
并且imageView在cell中,我还能直接展开全屏吗?
下面附上几张图片:
最佳答案
这是一个相当基本的解决方案。它假设您的集合单元有一个 UIImageView 作为 UICollectionViewCell contentView 的唯一 subview 。
#import <objc/runtime.h> // for objc_setAssociatedObject / objc_getAssociatedObject
...
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
UIImageView* iv = cell.contentView.subviews.lastObject;
UIImageView* ivExpand = [[UIImageView alloc] initWithImage: iv.image];
ivExpand.contentMode = iv.contentMode;
ivExpand.frame = [self.view convertRect: iv.frame fromView: iv.superview];
ivExpand.userInteractionEnabled = YES;
ivExpand.clipsToBounds = YES;
objc_setAssociatedObject( ivExpand,
"original_frame",
[NSValue valueWithCGRect: ivExpand.frame],
OBJC_ASSOCIATION_RETAIN);
[UIView transitionWithView: self.view
duration: 1.0
options: UIViewAnimationOptionAllowAnimatedContent
animations:^{
[self.view addSubview: ivExpand];
ivExpand.frame = self.view.bounds;
} completion:^(BOOL finished) {
UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
[ivExpand addGestureRecognizer: tgr];
}];
}
- (void) onTap: (UITapGestureRecognizer*) tgr
{
[UIView animateWithDuration: 1.0
animations:^{
tgr.view.frame = [objc_getAssociatedObject( tgr.view,
"original_frame" ) CGRectValue];
} completion:^(BOOL finished) {
[tgr.view removeFromSuperview];
}];
}
关于ios - 触摸时如何使图像扩展到全屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17744689/