我有一個imageView在scrollView中使用頁面控件現在我想顯示不同的圖像到同一個imageView。頁面滾動視圖控制滾動automatically.It顯示第一個圖像,但不顯示我已經使用了其他images.The代碼如下:如何在頁面控件的滾動視圖中將圖像應用於imageView。
// -----------------------------------------------------------------------------
// 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];
}
只張貼代碼你在哪裏設置你的第一個圖像。 – Gypsa 2011-06-07 07:58:47
註釋掉這一行// mStartPoint.x + = 250 + mPrevImgView.frame.size.width;然後嘗試 – Gypsa 2011-06-07 08:10:18