首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让图像在触摸时扩展到全屏?

问如何让图像在触摸时扩展到全屏?
EN

Stack Overflow用户
提问于 2013-07-19 19:11:15
回答 4查看 10.9K关注 0票数 8

我想做一个类似Facebook的iOS应用程序,当用户点击它时,它会从时间线上全屏显示图片。

更新:

我使用UICollectionView在单元格中显示图像,所以我似乎应该使用collectionView:didSelectItemAtIndexPath:方法?而且imageView在单元格中,所以我还可以直接扩展到全屏吗?

下面附上了几张图片:

​

​

​

​

EN

回答 4

Stack Overflow用户

发布于 2013-07-20 01:16:47

这是一个相当基本的解决方案。它假定您的集合单元格有一个UIImageView作为UICollectionViewCell contentView的唯一子视图。

代码语言:javascript
复制
#import <objc/runtime.h> // for objc_setAssociatedObject / objc_getAssociatedObject

..。

代码语言:javascript
复制
- (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];
                     }];
}
票数 13
EN

Stack Overflow用户

发布于 2013-07-19 19:14:00

使用UITapGestureRecognizer,别忘了设置imageView.userInteractionEnabled = YES;,因为默认情况下图像没有UserInteraction。

代码语言:javascript
复制
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
}
票数 5
EN

Stack Overflow用户

发布于 2013-07-19 19:28:43

我所理解的是,您在UIWebView中有您的图像这是我目前所拥有的,将UIWebViews委派设置为self,然后添加以下内容:

代码语言:javascript
复制
- (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;

我在这里发布的网页视图有一点小问题(只是为了以防你遇到一些问题!) Code.

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17744689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档