在UIscrollView中突出显示中间图像

时间:2011-07-29 05:47:45

标签: iphone objective-c

我正在创建一个应用程序,其中我将UIScrollView作为图像旋转器放在一个页面中,我们可以像图像滚动器一样看到两个图像。图像滚动为一种水平的方式。

现在问题是我想将中间图像显示为高亮显示。当图像围绕像素120时,图像将突出显示或弹出或类似选择。

请帮助我...... Thanx提前

1 个答案:

答案 0 :(得分:0)

您应该在旋转中使用两个图像视图,即将图像视图的图像设置为阵列中的下两个图像。这比使用10个imageview并用图像填充它们会节省大量内存。下面是一次显示一个图像的照片滚动条的代码,您应该能够根据需要更改框架宽度以显示两个。

一些常数:

//Portrait Image will have less width in landscape
#define kLandscapeImageHeight 300
#define kLandscapeImageWidth 208

//Landscape will have less height in portrait
#define kPortraitImageHeight 213
#define kPortraitImageWidth 320

#define kLandscapImageX 136
#define kPortraitImageY 134

初​​始化:

scrollView.contentSize = CGSizeMake([imageArray count]*320, scrollView.frame.size.height);
    if ([imageArray count] >= 1) {
        view1 = [[UIImageView alloc] init];  

        UIImage *view1Image = [imageArray objectAtIndex:imageToOpen];

        //Photo was taken in portrait
        if (view1Image.size.width < view1Image.size.height) {
            view1.frame = CGRectMake(imageToOpen*320, 0, 320, 460);
        }
        //Photo was taken in landscape
        else {
            view1.frame = CGRectMake(imageToOpen*320, kPortraitImageY, 320, kPortraitImageHeight);
        }
        view1.image = view1Image;
        [scrollView addSubview:view1];

        view2 = [[UIImageView alloc] init];

        if ([imageArray count] != 1) {
            if (imageToOpen < [imageArray count]-1) {

                UIImage *view2Image = [imageArray objectAtIndex:(imageToOpen+1)];

                //Photo was taken in portrait mode
                if (view2Image.size.width < view2Image.size.height) {
                    view2.frame = CGRectMake((imageToOpen+1)*320, 0, 320, scrollView.frame.size.height);
                }
                //Photo was taken in landscape mode
                else {
                    view2.frame = CGRectMake((imageToOpen+1)*320, kPortraitImageY, 320, kPortraitImageHeight);
                }
                view2.image = view2Image;
                [scrollView addSubview:view2];

            }
            else if (imageToOpen == [imageArray count]-1) {

                UIImage *view2Image = [imageArray objectAtIndex:(imageToOpen-1)];

                //Photo was taken in portrait mode
                if (view2Image.size.width < view2Image.size.height) {
                    view2.frame = CGRectMake((imageToOpen-1)*320, 0, 320, scrollView.frame.size.height);
                }
                //Photo was taken in landscape mode
                else {
                    view2.frame = CGRectMake((imageToOpen-1)*320, kPortraitImageY, 320, kPortraitImageHeight);
                }

                view2.image = [imageArray objectAtIndex:imageToOpen-1];
                [scrollView addSubview:view2];
            }

        }
        //For some reason, same function with animated 'NO' behaves differently!!!
        [scrollView scrollRectToVisible:view1.frame animated:YES];
    }

现在实现scrollView委托方法scrollViewDidScroll:在其中您将识别哪个图像将成为下一个图像并突出显示它。 'nextView'始终指向将在屏幕上显示的imageView。您可以将页面宽度减小到160,因为您希望一次显示两个图像:

- (void)scrollViewDidScroll:(UIScrollView *) scrollView {
    [self hideBars];

    UIInterfaceOrientation orientation = self.interfaceOrientation;
    CGFloat pageWidth;
    if (orientation == UIInterfaceOrientationPortraitUpsideDown || orientation == UIInterfaceOrientationPortrait) {
        pageWidth = 320;
    }
    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        pageWidth = 480;
    }
    float currPos = self.scrollView.contentOffset.x ;
    int selectedPage = roundf(currPos / pageWidth);    
    float truePosition = selectedPage*pageWidth;    
    int zone = selectedPage % 2;

    BOOL view1Active = zone == 0;

    UIImageView *nextView = view1Active ? view2 : view1;

    int nextpage = truePosition > currPos ? selectedPage-1 : selectedPage+1;

    if(nextpage >= 0 && nextpage < [imageArray count])
    {
        if((view1Active && nextpage == view1Index) || (!view1Active && nextpage == view2Index)) return;

        UIImage *nextViewImage = [imageArray objectAtIndex:nextpage];
        if (orientation == UIInterfaceOrientationPortraitUpsideDown || orientation == UIInterfaceOrientationPortrait) {
            if (nextViewImage.size.width < nextViewImage.size.height) {
                nextView.frame = CGRectMake(nextpage*320, 0, 320, 480);
            }
            else {
                nextView.frame = CGRectMake(nextpage*320, kPortraitImageY, 320, kPortraitImageHeight);
            }

        }
        else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
            if (nextViewImage.size.width < nextViewImage.size.height) {
                nextView.frame = CGRectMake(nextpage*480+kLandscapImageX, 0, kLandscapeImageWidth, kLandscapeImageHeight);
            }
            else {
                nextView.frame = CGRectMake(nextpage*480, 0, 480, 300);
            }

        }
        nextView.image = nextViewImage;

        if(view1Active) view1Index = nextpage;
        else view2Index = nextpage;
    }
}

如果您的UI支持两种方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;//(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);  

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    int selectedPage = 0;
    //if scrollview is scrolled till the last page
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        if (scrollView.contentOffset.x >= ([imageArray count]-1)*320) {
            selectedPage = [imageArray count]-1;
        }
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        if (scrollView.contentOffset.x >= ([imageArray count]-1)*480) {
            selectedPage = [imageArray count]-1;
        }
    }
    if (selectedPage != [imageArray count]-1) {
        //condition doesnt apply for last page
        if (view1.frame.origin.x < view2.frame.origin.x) {
            //View 1 is at the front
            selectedPage = [imageArray indexOfObject:view1.image];
        }
        else {
            //View 2 is at the front
            selectedPage = [imageArray indexOfObject:view2.image];

        }
    }

    int zone = selectedPage % 2;
    BOOL view1Active = zone == 0;

    UIImageView *thisView = view1Active ? view1 : view2;
    UIImageView *nextView = view1Active ? view2 : view1;

    NSLog(@"Selected Page = %d", selectedPage);

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        scrollView.contentSize = CGSizeMake([imageArray count]*480, kLandscapeImageHeight);

        if ([imageArray count] != 1) {
            if (/*thisView.frame.origin.x < nextView.frame.origin.x && */(selectedPage+1) != [imageArray count]) {
                UIImage *nextViewImage = [imageArray objectAtIndex:selectedPage+1];

                if (nextViewImage.size.width < nextViewImage.size.height) {
                    nextView.frame = CGRectMake((selectedPage+1)*480+kLandscapImageX, 0, kLandscapeImageWidth, kLandscapeImageHeight);
                }
                else {
                    nextView.frame = CGRectMake((selectedPage+1)*480, 0, 480, 300);
                }

                [nextView setImage:nextViewImage];
            }
            else {
                UIImage *nextViewImage = [imageArray objectAtIndex:selectedPage-1];

                if (nextViewImage.size.width < nextViewImage.size.height) {
                    nextView.frame = CGRectMake((selectedPage-1)*480+kLandscapImageX, 0, kLandscapeImageWidth, kLandscapeImageHeight);
                }
                else {
                    nextView.frame = CGRectMake((selectedPage-1)*480, 0, 480, 300);
                }
                [nextView setImage:nextViewImage];
            }

        }        
        UIImage *thisViewImage = [imageArray objectAtIndex:selectedPage];

        if (thisViewImage.size.width < thisViewImage.size.height) {
            //[thisView setFrame:CGRectMake((selectedPage*480)+kLandscapImageX, 0, kLandscapeImageWidth, kLandscapeImageHeight)];
            thisView.frame = CGRectMake((selectedPage*480)+kLandscapImageX, 0, kLandscapeImageWidth, kLandscapeImageHeight);

        }
        else {
            [thisView setFrame:CGRectMake(selectedPage*480, 0, 480, 300)];
        }

        [thisView setImage:thisViewImage];

        [scrollView setContentOffset:CGPointMake(selectedPage*480, 0) animated:YES];
    }

    else if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        scrollView.contentSize = CGSizeMake([imageArray count]*320, 460);

        if ([imageArray count] != 1) {
            if (/*thisView.frame.origin.x < nextView.frame.origin.x && */(selectedPage+1) != [imageArray count]) {
                UIImage *nextViewImage = [imageArray objectAtIndex:selectedPage+1];

                if (nextViewImage.size.width < nextViewImage.size.height) {
                    //[nextView setFrame:CGRectMake((selectedPage+1)*320, 0, 320, 460)];
                    nextView.frame = CGRectMake((selectedPage+1)*320, 0, 320, 460);

                }
                else {
                    nextView.frame = CGRectMake((selectedPage+1)*320, kPortraitImageY, 320, kPortraitImageHeight);
                }

                [nextView setImage:nextViewImage];
            }
            else {
                UIImage *nextViewImage = [imageArray objectAtIndex:selectedPage-1];

                if (nextViewImage.size.width < nextViewImage.size.height) {
                    [nextView setFrame:CGRectMake((selectedPage-1)*320, 0, 320, 460)];
                }
                else {
                    nextView.frame = CGRectMake((selectedPage-1)*320, kPortraitImageY, 320, kPortraitImageHeight);
                }

                [nextView setImage:nextViewImage];
            }

        }        
        UIImage *thisViewImage = [imageArray objectAtIndex:selectedPage];


        if (thisViewImage.size.width < thisViewImage.size.height) {
            [thisView setFrame:CGRectMake(selectedPage*320, 0, 320, 460)];
        }
        else {
            [thisView setFrame:CGRectMake(selectedPage*320, kPortraitImageY, 320, kPortraitImageHeight)];
        }
        [thisView setImage:thisViewImage];

        //For some reason, same function with animated 'NO' behaves differently!!!
        [scrollView setContentOffset:CGPointMake(selectedPage*320, 0) animated:YES];
        //this function doesnt work properly becase it doesnt dcroll if the rect is already visible. So photos load like half of one and half of the other
        //[scrollView scrollRectToVisible:CGRectMake(selectedPage*320, 0, 320, scrollView.frame.size.height) animated:YES];

    }   

}