2014-03-06 123 views
0

我想在我的UIImageView上實現滾動和縮放,但它只是垂直滾動。UIScrollView只能垂直滾動

它放大,它垂直滾動,所有東西都連接在InterfaceBuilder中。在InterfaceBuilder中沒有約束。

有人能幫我理解爲什麼嗎?

感謝,

// 
// O_GTab2ViewController3.m 
// Safety Moment 
// 
// Created by Fabio Lanza on 3/2/14. 
// Copyright (c) 2014 Fabio Lanza. All rights reserved. 
// 

#import "O_GTab2ViewController3.h" 

@interface O_GTab2ViewController3() <UIScrollViewDelegate> 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; 
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
@property (weak, nonatomic) IBOutlet UIImage *image; 

@end 

@implementation O_GTab2ViewController3 

@synthesize imageName; 



- (void)setScrollView:(UIScrollView *)scrollView { 

    scrollView.minimumZoomScale = 0.2; 
    scrollView.maximumZoomScale = 2.0; 

    scrollView.contentSize = self.imageView.image.size; 

} 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return self.imageView; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

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

    self.image = [UIImage imageNamed:imageName]; 
    self.imageView.image = self.image; 
    self.imageView.contentMode = UIViewContentModeTopLeft; 
    [self.imageView sizeToFit]; 

    self.scrollView.frame = CGRectMake(0, 64, 320, 455); 
    self.scrollView.contentSize = self.imageView.image.size; 
    self.scrollView.delegate = self; 

// [self.view addSubview:self.scrollView]; 

    [self.scrollView addSubview:self.imageView]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

回答

0

無論是滾動視圖將滾動取決於它的contentSize和frame.That是說,如果你的contentSize比框架大,它會滾動。

我檢查你的代碼:

scrollView.contentSize = self.imageView.image.size;

self.scrollView.frame = CGRectMake(0,64,320,455);

也許,imageView.size.height> 455,所以它會垂直滾動。

但是,imageView.size.width < 320,所以它不會水平拋出。

要水平滾動,您應該設置contentSize,寬度> 320.