2014-05-10 54 views
0

我試圖讓我的圖像爲panzoomable,但我似乎無法讓它工作。使用UIScrollView滾動UIImage

這裏是我的註釋代碼>創建UIScrollView>創建PFImageViewUIImage)>添加圖片到UIScrollView>獲取&顯示圖像。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    NSLog(@"%@",_courseObject);       //checking object has loaded 

    UIScrollView *scrollView = [[UIScrollView alloc] init]; 
    scrollView.frame = self.view.bounds;    //scroll view occupies full parent view! 
    scrollView.contentSize = CGSizeMake(400, 800);  //scroll view size 
    scrollView.showsVerticalScrollIndicator = YES;  //to hide scroll indicators! 
    scrollView.showsHorizontalScrollIndicator = YES; //by default, it shows! 
    scrollView.scrollEnabled = YES;      //say "NO" to disable scroll 
    [self.view addSubview:scrollView];     //adding to parent view! 



    PFImageView *mapImage = [[PFImageView alloc] init];   //create imageview 
    mapImage.frame = self.navigationController.view.frame;  //set image boundaries to fill nav frame(!) 
    mapImage.image = [UIImage imageNamed:@"loading"];   //placeholder image 
    mapImage.file = (PFFile *)[_courseObject objectForKey:@"file"]; //retrieve the map image 

    [scrollView addSubview:mapImage];        //add the mapImage to scrollView 
    [mapImage loadInBackground];         //retrieve image and add 

} 

回答

0

這裏的問題是,scrollView.contentSize是不是足夠大,因此試圖滾動它不拾取動作的時候。

更改值以填充框架以及實施委託協議允許我使用滾動視圖。

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds]; 
0

你需要使用一個滾動視圖代表,更具體,則需要實現這個方法:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 

(PFImageView * mapImage有可能成爲一個屬性,而不是一個局部變量)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 
NSLog(@"%@",_courseObject);       //checking object has loaded 

UIScrollView *scrollView = [[UIScrollView alloc] init]; 
scrollView.frame = self.view.bounds;    //scroll view occupies full parent view! 
scrollView.contentSize = CGSizeMake(400, 800);  //scroll view size 
scrollView.showsVerticalScrollIndicator = YES;  //to hide scroll indicators! 
scrollView.showsHorizontalScrollIndicator = YES; //by default, it shows! 
scrollView.scrollEnabled = YES;      //say "NO" to disable scroll 
[self.view addSubview:scrollView];     //adding to parent view! 
// HERE 
scrollView.delegate = self; 
// HERE 

mapImage = [[PFImageView alloc] init];   //create imageview 
mapImage.frame = self.navigationController.view.frame;  //set image boundaries to fill nav frame(!) 
mapImage.image = [UIImage imageNamed:@"loading"];   //placeholder image 
mapImage.file = (PFFile *)[_courseObject objectForKey:@"file"]; //retrieve the map image 

[scrollView addSubview:mapImage];        //add the mapImage to scrollView 
[mapImage loadInBackground];         //retrieve image and add 

} 


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return mapImage; 
}