我想制作一个类似 Facebook 的 iOS 应用程序,当用户点击它时,它会从时间轴全屏显示一张图片。
更新:
我正在使用 UICollectionView 在单元格中显示图像,所以似乎我应该使用collectionView:didSelectItemAtIndexPath:
方法?并且imageView在单元格中,所以我仍然可以直接扩展到全屏吗?
下面附上几张图片:
这是一个相当基本的解决方案。它假定您的集合单元格有一个 UIImageView 作为 UICollectionViewCell contentView 的唯一子视图。
#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];
}];
}
使用UITapGestureRecognizer
,并且不要忘记设置imageView.userInteractionEnabled = YES;
,因为默认情况下图像没有用户交互。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
//code for full screen image
}
你是对的,使用 UICollectionView 的委托方法:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//get UIImage from source
//show (add subView) UIImageView with full screen frame
//add gesture for double tap on which remove UIImageView
}
我的理解是你在 UIWebView 中有你的图像这是我目前拥有的,将 UIWebViews 委托设置为 self 然后添加:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
ImageViewer *imageView = [[[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil] autorelease];
imageView.imageURL = URL;
[self presentModalViewController:imageView animated:YES];
return NO;
} else {
return YES;
}
}
只需像往常一样将图像作为 html 中的链接。如果您有其他不想加载到模型中的链接,那么您可以修改代码以检测被按下的链接是否指向图像,否则只返回 YES;
我在此处发布的 webview 有一点问题(以防万一您遇到问题!!)代码。
希望这可以帮助!