2011-06-29 62 views
1

嗨,我有一個大的圖像和它在一個特定的位置的小圖像... 我將它們全部添加到ScrollView下,但它不能正常工作? 我要放大,一起各在其位我怎樣才能使用UIScrollView重疊圖像iphone

這裏是我的代碼:

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; 
}  

回答

0

你實現:

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

而且,你知道,在滾動視圖的contentSize設置爲您IMAGESIZE,它不能長得比任何更大的,對不對?它會放大位置,相應地裁剪外部像素。另外,如果這是您的計劃,爲什麼不直接設置?

myScrollView.contentSize = imageView.frame.size; 

=====另外的答案,應對額外的源

好吧,我發現的主要事情是,你需要在viewForZoomingInScrollView,而不是滾動視圖返回ImageView的ucdView。看起來你已經創建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>協議,您的ViewController定義(在.h文件中)?

最後,你有一個scrollview和健康泄漏;添加[scrollView發佈];和[健康發佈]在視圖底部DodLoad

+0

HI馬克沃斯可以請你檢查我張貼上面的答案,,, – Dalal

+0

請參閱我的答案的編輯版本。另外,恐怕我不能完全解析「我做了它,但如果我沒有最大化圖像,我無法在任何地方移動滾動視圖,但是如果我放大它,我可以看到其餘的..我不知道我爲什麼試圖改變它,但它工作。「也許再拍一下? – mackworth

+0

嗨麥克,感謝您的答覆百萬,,,我修復了代碼,你的幫助我很多.... – Dalal