在UIScrollView实例中创建图像放大

时间:2012-01-10 13:37:55

标签: iphone objective-c uiscrollview magnification

我有一个UIScrollView实例,里面有图像。 可以水平滚动滚动视图,并查看更多图像。 我想根据它们与滚动视图中间的距离来放大图像,因此它应该看起来像this

我想到了最简单的方法,使用以下代码:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    //Change the frames of all the visible images here.
}

这种方式在某些前面的设备上无法顺利运行,例如iPod 2G。

有没有比我描述的更好的方法来执行图像放大? 如果是的话,请告诉我。

谢谢!


编辑:

这是上述方法体的实现:

-(void) magnifyImages {
    //This method magnifies all the images.
    for (NSString *name in [imagesDict allKeys]) {
        UIImageView *imageView = [imagesDict objectForKey:name];
        [self magnifyImageView:imageView];
    }
}

static float kMaxMagnification = 1.5;

-(void) magnifyImageView:(UIImageView *)imageView {
    CGRect frame = imageView.frame;
    float imageMiddleLine = frame.origin.x+frame.size.width/2-scrollView.contentOffset.x;
    //Check if the image's middle line is visible = whether it is needed to magnify the image.
    if (imageMiddleLine +frame.size.width/2>0
    && imageMiddleLine-frame.size.width/2<scrollView.frame.size.width) {
        float magnification = fabs(160-imageMiddleLine);
        //Mathematical formula that calculates the magnification.
        magnification = (kMaxMagnification-1)*(kDeviceWidth/2-magnification)/160+1;
        //'normalImageSize' is a property of the image that returns the image's normal size (when not magnified).
        CGSize imgSize = imageView.normalImageSize;
        frame=CGRectMake(frame.origin.x-(imgSize.width*magnification-frame.size.width)/2,
                     frame.origin.y-(imgSize.height*magnification-frame.size.height)/2, 
                     imgSize.width*magnification, imgSize.height*magnification);
        imageView.frame=frame;
    }
}

0 个答案:

没有答案