0

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

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

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

0 回答 0