UIScrollView只能垂直滚动

时间:2014-03-06 23:09:48

标签: ios7 uiscrollview scroll uiimageview zoom

我正在尝试在我的UIImageView上实现滚动和缩放,但它只是垂直滚动。

它缩放,它垂直滚动,所有内容都在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

1 个答案:

答案 0 :(得分:0)

滚动视图是否会滚动取决于其contentSize和frame。也就是说,如果你的contentSize大于frame,它会滚动。

我检查了你的代码:

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

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

也许,imageView.size.height&gt; 455,所以它会垂直滚动。

但是,imageView.size.width&lt; 320,所以它不会水平划伤。

要水平滚动,您应设置contentSize,width&gt; 320。