如何将图像缩放到全屏模式?

时间:2012-03-07 09:12:54

标签: iphone objective-c ios animation zooming

我有图片分页UIScrollView。我想在一些手势上实现图像的动画缩放到全屏。另一个动画将图像缩放回滚动视图。就像在iPad上的照片应用程序或Safari中的视频一样。

当然不会是UIImageView。它将是一些包含图像的包装类。主要问题是如何呈现全屏视图。是否必须是模态视图。

感谢任何帮助。

4 个答案:

答案 0 :(得分:6)

检查触摸图像 如果是小转换成全尺寸。 如果是大转换成小尺寸。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (isLarge) [self makeSmall];
    else [self makeFull];
}

- (void)makeFull {
    [[self superview] bringSubviewToFront:self];
    isLarge = YES;
    CGRect largeFrame = [self superview].bounds;
    original = self.frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [self setFrame:largeFrame];
    [internal setFrame:self.bounds];
    [UIView commitAnimations];

}
- (void)makeSmall {
    isLarge = NO;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [self setFrame:original];
    [internal setFrame:self.bounds];
    [UIView commitAnimations];

}

答案 1 :(得分:3)

我找到了一个涉及手势识别器和动画过渡的解决方案。您删除滚动视图并添加全屏图像。然后反过来返回到只是滚动视图。

使用窗口框架确定新UIImage的大小。

可以调整手势识别器的类型以满足您的需求!

-(void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapTwice:)];
    tapTwice.numberOfTapsRequired = 2;
    [self.imageView addGestureRecognizer:tapTwice];
}

- (void)tapOnce:(id)tapOnce {
    [UIView transitionWithView:self.view
                  duration:0.7
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^ {
                    [self.fullScreenImage removeFromSuperview];
                }
                completion:nil];
}

- (void)tapTwice:(id)tapTwice {
    if(self.fullScreenImage == nil){
        self.fullScreenImage = [[UIImageView alloc] initWithFrame:self.view.window.frame];

        UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapOnce:)];
        tapOnce.numberOfTapsRequired = 1;
        [tapOnce requireGestureRecognizerToFail:tapTwice];
        [self.fullScreenImage addGestureRecognizer:tapOnce];
        [self.fullScreenImage setUserInteractionEnabled:YES];

        //self.fullScreenImage = [load your image here]
    }

    [UIView transitionWithView:self.view.window
                      duration:0.7
                       options:UIViewAnimationOptionTransitionCrossDissolve 
                    animations:^ {
                        [self.view.window addSubview:self.fullScreenImage];
                    }
                    completion:nil];
}

答案 2 :(得分:2)

为图片库示例代码下载此sample并完成该操作。它肯定会帮助您了解如何实现您的想法。

答案 3 :(得分:1)

如下所示,您可以使用缩放图像视图。

UIView * viewForScroll;

UIImageView *imgviewForScroll;

UIScrollView *scrollToRead = [[UIScrollViewalloc]initWithFrame:CGRectMake(175, 5, 300, 311)];

scrollToRead.backgroundColor = [UIColordarkGrayColor];

scrollToRead.showsHorizontalScrollIndicator = YES;

scrollToRead.delegate = self;

[scrollToRead setContentSize:CGRectMake(0, 0, 300, 400).size];

scrollToRead.maximumZoomScale = 2;

scrollToRead.minimumZoomScale = 1;

[self.view addSubview:scrollToRead];  

[scrollToRead release]; 

viewForScroll = [[UIViewalloc]initWithFrame:CGRectMake(0,0,300,400)];

[scrollToRead addSubview:viewForScroll];

[viewForScroll release];

imgviewForScroll = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,300,400)];

imgviewForScroll.image = [UIImageimageWithContentsOfFile:[arrayComicContentobjectAtIndex:0]];

[viewForScroll addSubview:imgviewForScroll];

[imgviewForScroll release];

// ========= UIScrollView委托方法

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {


    return viewForScroll;
    }

想知道您是否需要进一步澄清