如何使用UIScrollView重叠图像iphone

时间:2011-06-29 18:45:11

标签: iphone image uiscrollview overlapping

嗨,我在一个特定的位置有一个很大的图像和小图像... 我在ScrollView下添加了它们,但它无法正常工作? 我想放大,每个都放在一起

这是我的代码:

    myScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    myScrollView.maximumZoomScale = 3.0;
    myScrollView.minimumZoomScale = 1.0;
    myScrollView.clipsToBounds = YES;
    myScrollView.delegate = self;

    [myScrollView addSubview:imageView];

感谢


嗨,麦克沃思;我做到了,但如果我没有最大化图像,我无法将滚动视图移动到任何地方,但如果我放大它,我可以看到其余的...我不知道为什么我尝试更改它,但它工作。你能看一下下面的代码吗?

- (void)viewDidLoad{
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"ucd.png"];
    //creating a view for UCD image
    ucdView = [[UIImageView alloc] initWithImage:image];
    //setting the frame for the ucdView as the same size of ucd image
    [ucdView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];


    //for the red pin to display in specific loc
    UIImageView *Health = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redPin.png"]];
    [Health setCenter:CGPointMake(310,135)];
    //adding helathView to ucdview
    [ucdView addSubview:Health];
    //everything for scroll view
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ucdView.frame.size.width, ucdView.frame.size.height)];
    [scrollView setMinimumZoomScale:1.0];
    [scrollView setMaximumZoomScale:3.0];
    scrollView.clipsToBounds = YES;
    [scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
    [scrollView setDelegate:self];
    [scrollView addSubview:ucdView];//adding ucd view to scroll view
    [self.view addSubview:scrollView]; // adding scrollview to self.view main view
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {//apply zooming for scrollview
    return scrollView;
}    

1 个答案:

答案 0 :(得分:0)

您是否实施了:

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

另外,你知道通过将scrollView的contentSize设置为你的imageSize,它不会比那更大,对吗?它会放大到位,相应地裁剪外部像素。顺便说一句,如果那是你的计划,为什么不直接设置呢?

myScrollView.contentSize = imageView.frame.size;

=====进一步回答,回复其他来源

嗯,我注意到的主要事情是你需要在viewForZoomingInScrollView中返回imageView ucdView,而不是scrollView。看起来你已经创建了ucdView作为一个属性(所以只要你的视图存在它就会存在),但以防万一......

1)在你的.h文件中:

UIImageView * ucdView;
@property (nonatomic, retain) UIImageView * ucdView;

2)在您的.m文件中;

@synthesize ucdView;

3)改变你的路线:

ucdView = [[UIImageView alloc] initWithImage:image];

 self.ucdView = [[UIImageView alloc] initWithImage:image];

4)改变行

 return scrollView

为:

 return self.ucdView;

5)将以下内容添加到dealloc例程中:

self.ucdView = nil;

另外,您添加了<UIScrollViewDelegate&gt;您的ViewController定义的协议(在.h文件中)?

最后,你有一个scrollview和Health的泄漏;添加[scrollView release];和viewDidLoad

末尾的[健康发布]