2015-08-21 80 views
1

嗨,我是初學者在iOS和在我的項目我在UIScrollView中添加UIImage的,我已經在UIImage的如何獲得原始圖像時,我們放大和IOS縮小圖像

當我們加倍增加雙擊手勢點擊UIImage然後圖像應該在視圖控制器上縮放全屏大小

在完整屏幕大小的圖像後,我們可以像想要的任何方式縮放它(我的意思是使用像捏縮放效果)在這裏我的要求是當我們雙倍點擊圖像然後圖像需要設置它的原始位置我已經嘗試我的水平最好但我沒有得到結果請幫助我

我的代碼如下:

#import "ViewController2.h" 

@interface ViewController2() 
{ 
    UIScrollView * myScroll; 
    UITapGestureRecognizer *tap; 
    BOOL isFullScreen; 
    CGRect prevFrame; 
    UIImageView * _imageView; 
} 

@end 

@implementation ViewController2 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    isFullScreen = FALSE; 
    myScroll = [[UIScrollView alloc] init]; 
    myScroll.frame = self.view.bounds; 
    myScroll.contentSize = CGSizeMake(_imageView.frame.size.width, _imageView.frame.size.height); 
    myScroll.maximumZoomScale = 4.0; 
    myScroll.minimumZoomScale = 1.0; 
    myScroll.clipsToBounds = YES; 
    myScroll.delegate = self; 
    myScroll.backgroundColor = [UIColor lightGrayColor]; 
    [self.view addSubview:myScroll]; 

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)]; 
    _imageView.contentMode = UIViewContentModeScaleAspectFill; 
    [_imageView setClipsToBounds:YES]; 
    _imageView.userInteractionEnabled = YES; 
    _imageView.image = [UIImage imageNamed:@"ram.jpeg"]; 

    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)]; 
    tapper.numberOfTapsRequired = 1; 
    [_imageView addGestureRecognizer:tapper]; 
    [myScroll addSubview:_imageView]; 
} 

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

    return _imageView; 
} 

-(void)imgToFullScreen:(UITapGestureRecognizer*)sender { 

    if (!isFullScreen) { 

     [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 

      prevFrame = _imageView.frame; 
      [_imageView setFrame:[[UIScreen mainScreen] bounds]]; 
     }completion:^(BOOL finished){ 
      isFullScreen = TRUE; 
     }]; 
     return; 
    } 
    else{ 
     [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 
      [_imageView setFrame:prevFrame]; 
     }completion:^(BOOL finished){ 
      isFullScreen = FALSE; 
     }]; 
     return; 
    } 
} 

@end 

回答

0

我做了一些修改,在你的代碼像下面試試吧。檢查它給你想要的輸出與否。

@interface ScrollViewController()<UIScrollViewDelegate>{ 
     UIScrollView * myScroll; 
     UITapGestureRecognizer *tap; 
     BOOL isFullScreen; 
     CGRect prevFrame; 
     UIImageView * _imageView; 
     CGAffineTransform trans; 
    } 



    @end 

    @implementation ScrollViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view from its nib. 

     isFullScreen = FALSE; 
     myScroll = [[UIScrollView alloc] init]; 
     myScroll.frame = [UIScreen mainScreen].bounds; 
     myScroll.contentSize = CGSizeMake(prevFrame.size.width, prevFrame.size.height); 
     myScroll.maximumZoomScale = 4.0; 
     myScroll.minimumZoomScale = 1.0; 
     myScroll.clipsToBounds = YES; 
     myScroll.delegate = self; 
     myScroll.backgroundColor = [UIColor lightGrayColor]; 
     [self.view addSubview:myScroll]; 

     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)]; 
     _imageView.contentMode = UIViewContentModeScaleAspectFill; 
     [_imageView setClipsToBounds:YES]; 
     _imageView.userInteractionEnabled = YES; 
     _imageView.image = [UIImage imageNamed:@"img.png"]; 

     UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)]; 
     tapper.numberOfTapsRequired = 2; 
     [_imageView addGestureRecognizer:tapper]; 
     [myScroll addSubview:_imageView]; 

     prevFrame = _imageView.frame; 
     trans = _imageView.transform; 
    } 

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

     return _imageView; 
    } 

    -(void)imgToFullScreen:(UITapGestureRecognizer*)sender { 


     if (!isFullScreen) { 
      myScroll.contentSize = prevFrame.size; 
      [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 
       _imageView.frame = [UIScreen mainScreen].bounds; 

      }completion:^(BOOL finished){ 
       isFullScreen = TRUE; 

      }]; 
      return; 
     } 
     else{ 
      [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 

       myScroll.contentSize = CGSizeMake(prevFrame.size.width, prevFrame.size.height); 
       _imageView.transform = trans; 
       _imageView.frame = prevFrame; 


       NSLog(@"%@ %@",NSStringFromCGSize(myScroll.contentSize),NSStringFromCGRect(prevFrame)); 
      }completion:^(BOOL finished){ 
       isFullScreen = FALSE; 

      }]; 
      return; 
     } 
    } 
+0

由於做工精細 – Krish

+0

大這個答案解決了我的問題,謝謝 –

+0

但是當我放大它的圖像模糊,我們如何解決這個問題 –

相關問題