触摸后如何将图像扩展为全屏?

时间:2013-07-19 11:11:15

标签: ios uiimage

我想创建一个类似Facebook的iOS应用程序,当用户点击它时,它会从时间轴全屏显示。

更新

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

附上几张图片:

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:13)

这是一个相当基本的解决方案。它假设您的集合单元格具有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];
                     }];
}

答案 1 :(得分:5)

使用UITapGestureRecognizer,不要忘记设置imageView.userInteractionEnabled = YES;,因为默认图片没有UserInteraction。

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
}

答案 2 :(得分:0)

我的理解是您在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;

我在我的网页浏览中遇到了一些问题(只是因为你遇到了一些问题!)Code.

希望这有帮助!

答案 3 :(得分:0)

你是对的,使用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
}