使用UIScrollView滚动UIImage

时间:2014-05-10 12:14:20

标签: ios uiscrollview uiimageview

我正在尝试允许我的图片为panzoomable,但我似乎无法让它工作。

这是我的评论代码>创建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

}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

这里的问题是scrollView.contentSize不够大,所以当试图滚动它时没有采取动作。

更改值以填充框架以及实现委托协议允许我使用滚动视图。

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