0

我正在开发一个应用程序。在这里,我描述了我遇到的一个奇怪问题的简化版本。

我有一个情节提要创建的视图控制器,其中 UIImageView 也在情节提要中创建。

然后我有以下 h 和 m 文件,旨在将 UIScrollView 添加到 UIImageview 并为滚动视图提供容器。

h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) UIScrollView* scrollView;
@property (nonatomic, strong) UIView* container;

@end

.m 文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed: @"new_phots_fake_port.png"];
    //Add the image to the imageView I created in the Storyboard
    [_imageView setImage:image];


    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60.0, 70.0, 522.0, 100.0)];
    [_scrollView setBackgroundColor:[UIColor yellowColor]];


    // Set up the container view to exist inside the scrollview:
    CGSize containerSize = CGSizeMake(5000.0f, 730.0f);
    _container = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f),       .size=containerSize}];
    _container.backgroundColor = [UIColor redColor];
    // Tell the scroll view the size of the contents
    self.scrollView.contentSize = containerSize;
    [_scrollView addSubview:_container];


    [_imageView addSubview:_scrollView]; //This is what I want but doesn't allow scrolling

    //[self.view addSubview:_scrollView];  //This allows scrolling but makes the imageView      and Scrollview siblings.  I need scrollview to be a child of imageview
   }

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

@end

因此,滚动视图似乎无法添加到图像视图中(并且功能正常)。 为什么?我究竟做错了什么?

奇怪的是,我也没有使用任何委托方法 viewForZoomingInScrollView 或 scrollViewDidZoom。我不确定为什么不需要这些。

4

1 回答 1

1

您需要在UIImageView

_imageView.userInteractionEnabled = YES;
于 2012-10-05T23:06:40.840 回答