简体   繁体   English

UIScrollview使当前图像变大

[英]UIScrollview make the current Image Larger

I am building a scrollable gallery with 10 images. 我正在建立一个包含10张图像的可滚动画廊。 When I stop on an image, I want the current image to look little taller than the other ones on either side. 当我停在图像上时,我希望当前图像看起来比两侧的其他图像高一点。 How do I get the view in focus and adjust the frame ? 如何聚焦视图并调整框架? Once the drag ends, the function scrollViewDidEndDecelerating gets called. 拖动结束后,将调用函数scrollViewDidEndDecelerating。 How do I get the reference to that imageview's frame ? 如何获得对该imageview框架的引用?

Let's say that you hold reference to the UIImageView s in self.imageViews , you can try something like this. 假设您在self.imageViews保留对UIImageView的引用,则可以尝试这样的操作。

CGFloat centerX = CGRectGetMidX(self.view.bounds);
for(UIImageView imgView * in self.imageViews)
{
    if(CGRectContainsPoint(imgView, centerX)) // If imgView is close to center
    {
        imgView.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
    }
    else
    {
        imgView.transform = CGAffineTransformIdentity;
    }
}

I assumed you are scrolling horizontally, but if you are doing it vertically, you need to use centerY instead. 我假设您正在水平滚动,但是如果您垂直滚动,则需要使用centerY

查看iCarousel,而不是重新发明轮子。... https://github.com/nicklockwood/iCarousel

If your list of images is finite, and won't get too large, I'd keep an NSArray of references to the images. 如果您的图片列表是有限的,并且不会太大,那么我将保留对图片的NSArray引用。 Then, in scrollViewDidEndDecelerating you can use the scroll offset to determine where the scrolling stopped, and put that in proportion to the whole view, and you should be able to tell which index'd image in the array to show. 然后,在scrollViewDidEndDecelerating ,可以使用滚动偏移量确定滚动停止的位置,并将其与整个视图成比例,并且应该能够确定要显示数组中哪个索引图像。

Something like this (syntax might be off) 这样的事情(语法可能已关闭)

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //assumes that "arrayOfImages" is the array of images that you are showing
    //contentOffset.y assumes your scroll view is scrolling vertically, switch to x if horizontal
    int indexOfImageToShow = scrollView.contentOffset.y / [arrayOfImages count];
    UIImage *imageToShow = [arrayOfImages objectAtIndex:indexOfImageToShow];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM