为什么我不能将UIScrollView添加到UIImageView(使其正常工作)

时间:2012-10-05 22:59:18

标签: objective-c uiscrollview uiimageview storyboard uiscrollviewdelegate

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

我创建了一个故事板,在故事板中创建了一个带有UIImageView的View Controller。

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

h file:

#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 file:

#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

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

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

1 个答案:

答案 0 :(得分:1)

您需要在UIImageView

上设置userInteractionEnabled
_imageView.userInteractionEnabled = YES;