如何将图像应用到 pagecontrol 中滚动视图中的 imageView。

发布于 2024-11-14 08:41:29 字数 4829 浏览 3 评论 0原文

我使用页面控件在scrollView中有一个imageView,现在我想将不同的图像显示到同一个imageView。页面控制scrollview自动滚动。它显示第一张图像,但不显示其他图像。我使用的代码如下:

// -----------------------------------------------------------------------------
// loadScrollViewWithPage

- (void)loadScrollViewWithPage:(int)page 
{
    if ( page < 0 ) return;
    if ( page >= kNumberOfPages ) return;
}

// -----------------------------------------------------------------------------
// scrollViewDidScroll

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{
    if ( pageControlUsed ) 
    {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = mScrollView.frame.size.width;
    int page = floor((mScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    mPageControl.currentPage = page;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
}

// -----------------------------------------------------------------------------
// scrollViewWillBeginDragging
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{
    pageControlUsed = NO;
    [mTimer invalidate];
}

// ----------------------------------------------------------------
// scrollViewDidEndDecelerating
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    pageControlUsed = NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil 
                  tag:(NSInteger)inTag
            noOfPoses:(NSInteger)inNoOfPoses
            imageName:(NSArray *)inImageNameArr
{
    self = [super initWithNibName:@"TIphonePosesDetailView" bundle:nibBundleOrNil];
    if (self) 
    {
        mTag = inTag;// tag of the image taken from previous controller
        mNoOfPoses = inNoOfPoses;
        kNumberOfPages = mNoOfPoses;
        if ( inImageNameArr != nil ) 
        {
            mImgNameArr = [inImageNameArr retain];// contains images
        }
        NSLog(@"Image Array%@",mImgNameArr);

    }
    return self;
}

// -----------------------------------------------------------------------------
// viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    mScrollView.delegate = self;
    mScrollView.showsHorizontalScrollIndicator = FALSE;
    [mScrollView setContentSize:CGSizeMake(1000, 464)];
    [mScrollView setFrame:CGRectMake(45, 69, 240, 226)];

    // Setting frame
    mStartPoint = CGPointMake(0, 0);
    [mPrevImgView setImage:[self getImageFromName:[mImgNameArr objectAtIndex:mTag]]];
    [mPrevImgView setFrame:CGRectMake(mStartPoint.x, mStartPoint.y, 240, 226)];

    // a page is the width of the scroll view
    mScrollView.pagingEnabled = YES;
    mScrollView.contentSize = CGSizeMake(mScrollView.frame.size.width * kNumberOfPages, mScrollView.frame.size.height);
    mScrollView.showsHorizontalScrollIndicator = NO;
    mScrollView.showsVerticalScrollIndicator = NO;
    mScrollView.scrollsToTop = NO;
    mPageControl.numberOfPages = kNumberOfPages;
    mPageControl.currentPage = 0;

    // pages are created on demand
    // load the visible page
    // load the page on either side to avoid flashes when the user starts scrolling
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
    mTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:2] interval:2.0 target:self selector:@selector(scrollAutomatically:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:mTimer forMode:NSDefaultRunLoopMode];
}

// -----------------------------------------------------------------------------
// scrollAutomatically

- (void)scrollAutomatically:(id)sender
{  
    if ( mCurrentPage < [mPageControl numberOfPages] )
    {
        mCurrentPage ++;
        mTag ++;
        [mPrevImgView setImage:[self getImageFromName:[mImgNameArr objectAtIndex:mTag]]];
        mStartPoint.x += 250 + mPrevImgView.frame.size.width; 
    }
    else 
    {
        mCurrentPage = 0;
    }

    [self loadScrollViewWithPage:mCurrentPage - 1];
    [self loadScrollViewWithPage:mCurrentPage];
    [self loadScrollViewWithPage:mCurrentPage + 1];

    // update the scroll view to the appropriate page
    CGRect frame = mScrollView.frame;
    frame.origin.x = frame.size.width * mCurrentPage;
    frame.origin.y = 0;
    [mScrollView scrollRectToVisible:frame animated:YES];
}

I have one imageView in scrollView using page control now I want to display different images to the same imageView. The page control scrollview scroll automatically.It shows the first image but not showing the other images.The code I have used follows:

// -----------------------------------------------------------------------------
// loadScrollViewWithPage

- (void)loadScrollViewWithPage:(int)page 
{
    if ( page < 0 ) return;
    if ( page >= kNumberOfPages ) return;
}

// -----------------------------------------------------------------------------
// scrollViewDidScroll

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{
    if ( pageControlUsed ) 
    {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = mScrollView.frame.size.width;
    int page = floor((mScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    mPageControl.currentPage = page;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
}

// -----------------------------------------------------------------------------
// scrollViewWillBeginDragging
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{
    pageControlUsed = NO;
    [mTimer invalidate];
}

// ----------------------------------------------------------------
// scrollViewDidEndDecelerating
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    pageControlUsed = NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil 
                  tag:(NSInteger)inTag
            noOfPoses:(NSInteger)inNoOfPoses
            imageName:(NSArray *)inImageNameArr
{
    self = [super initWithNibName:@"TIphonePosesDetailView" bundle:nibBundleOrNil];
    if (self) 
    {
        mTag = inTag;// tag of the image taken from previous controller
        mNoOfPoses = inNoOfPoses;
        kNumberOfPages = mNoOfPoses;
        if ( inImageNameArr != nil ) 
        {
            mImgNameArr = [inImageNameArr retain];// contains images
        }
        NSLog(@"Image Array%@",mImgNameArr);

    }
    return self;
}

// -----------------------------------------------------------------------------
// viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    mScrollView.delegate = self;
    mScrollView.showsHorizontalScrollIndicator = FALSE;
    [mScrollView setContentSize:CGSizeMake(1000, 464)];
    [mScrollView setFrame:CGRectMake(45, 69, 240, 226)];

    // Setting frame
    mStartPoint = CGPointMake(0, 0);
    [mPrevImgView setImage:[self getImageFromName:[mImgNameArr objectAtIndex:mTag]]];
    [mPrevImgView setFrame:CGRectMake(mStartPoint.x, mStartPoint.y, 240, 226)];

    // a page is the width of the scroll view
    mScrollView.pagingEnabled = YES;
    mScrollView.contentSize = CGSizeMake(mScrollView.frame.size.width * kNumberOfPages, mScrollView.frame.size.height);
    mScrollView.showsHorizontalScrollIndicator = NO;
    mScrollView.showsVerticalScrollIndicator = NO;
    mScrollView.scrollsToTop = NO;
    mPageControl.numberOfPages = kNumberOfPages;
    mPageControl.currentPage = 0;

    // pages are created on demand
    // load the visible page
    // load the page on either side to avoid flashes when the user starts scrolling
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
    mTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:2] interval:2.0 target:self selector:@selector(scrollAutomatically:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:mTimer forMode:NSDefaultRunLoopMode];
}

// -----------------------------------------------------------------------------
// scrollAutomatically

- (void)scrollAutomatically:(id)sender
{  
    if ( mCurrentPage < [mPageControl numberOfPages] )
    {
        mCurrentPage ++;
        mTag ++;
        [mPrevImgView setImage:[self getImageFromName:[mImgNameArr objectAtIndex:mTag]]];
        mStartPoint.x += 250 + mPrevImgView.frame.size.width; 
    }
    else 
    {
        mCurrentPage = 0;
    }

    [self loadScrollViewWithPage:mCurrentPage - 1];
    [self loadScrollViewWithPage:mCurrentPage];
    [self loadScrollViewWithPage:mCurrentPage + 1];

    // update the scroll view to the appropriate page
    CGRect frame = mScrollView.frame;
    frame.origin.x = frame.size.width * mCurrentPage;
    frame.origin.y = 0;
    [mScrollView scrollRectToVisible:frame animated:YES];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2024-11-21 08:41:29

当您调用此方法时,通过动态固定坐标来加载所有图像。您可以使用缓存来加载图像,这将节省加载时间并可用

- (void)loadScrollViewWithPage:(int)page 
{
    if ( page < 0 ) return;
    if ( page >= kNumberOfPages ) return;
}

,但在加载视图时仅显示第一页

之后,ScrollView委托方法将在移动页面控件时起作用......

但停止调用这些

 [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

它将重复创建图像

When you are calling to this method load all images by fixing their co-ordinates dynamically .you can use cache to load images which will save time in loading and available here

- (void)loadScrollViewWithPage:(int)page 
{
    if ( page < 0 ) return;
    if ( page >= kNumberOfPages ) return;
}

but show only first page when view is loaded

After that ScrollView delegate method will work in moving you page control....

but stop call to these

 [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

it will create images repeatedly

世界和平 2024-11-21 08:41:29

http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007795

此链接可能对您有帮助。

调用这个函数来代替这个函数:

+(UIImage *)pageControlColorWithIndex:(NSUInteger)index
{
}

使用这个

+(UIImage *)pageControlImageWithIndex:(NSUInteger)索引
{
if (__pageControlColorList == nil)
{
__pageControlColorList = [[NSArray alloc] initWithObjects:[UIImage > imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"], [UIImage imageNamed:@"5.png"], [UIImage >imageNamed:@"6.png"], [UIImage imageNamed:@"7.png"],无];
}

返回[_pageControlColorList objectAtIndex:index % [_pageControlColorList count]];
}

在 MyViewController 类中添加 UIScrollView 对象。在 ViewDidLoad 中调用这个

mImage.image = [MyViewController pageControlImageWithIndex:pageNumber];

http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007795

This link might helps you.

Instead of this function call this :

+(UIImage *)pageControlColorWithIndex:(NSUInteger)index
{
}

Use this method

+(UIImage *)pageControlImageWithIndex:(NSUInteger)index
{
if (__pageControlColorList == nil)
{
__pageControlColorList = [[NSArray alloc] initWithObjects:[UIImage >imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"], [UIImage imageNamed:@"5.png"], [UIImage >imageNamed:@"6.png"], [UIImage imageNamed:@"7.png"], nil];
}

return [_pageControlColorList objectAtIndex:index % [_pageControlColorList count]];
}

And add UIScrollView Object in MyViewController class. And in ViewDidLoad call this

mImage.image = [MyViewController pageControlImageWithIndex:pageNumber];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文