if(self){
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.contentSize = CGSizeMake(320, 465);
[scrollView setScrollEnabled:YES];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:NO];
[self.uiSubView addSubview:scrollView];
NSMutableArray *arrImage = [NSMutableArray arrayWithObjects:@"a.jpeg", @"cat.jpg", @"s.jpeg",@"ss.jpeg", nil];
for (int i = 0; i < [arrImage count]; i++)
{
CGFloat xOrigin = i * scrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
[imageView setImage:[UIImage imageNamed:[arrImage objectAtIndex:i]]];
[imageView setContentMode: UIViewContentModeScaleAspectFit];
[scrollView addSubview:imageView];
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [arrImage count], scrollView.frame.size.height)];
}
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(scrollPages)
userInfo:nil
repeats:NO];
- (void)scrollPages
{
//I want a method here, the timer works for the first image later it will not scroll, so tel me how to introduce delay between images.
}
答案 0 :(得分:1)
在viewWillAppear:
_slideShowTimer = [NSTimer scheduledTimerWithTimeInterval:K_DEFINE_YOUR_TIME
target:self
selector:@selector(scrollPages)
userInfo:Nil
repeats:YES];
-(void)scrollPages {
if (currentPage>=self.pageControl.numberOfPages) {
currentPage=0;
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
[self scrollToPage:currentPage];
currentPage++;
}
-(void)scrollToPage:(NSInteger)aPage {
float myPageWidth = [self.scrollView frame].size.width;
[self.scrollView setContentOffset:CGPointMake(aPage*myPageWidth,0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
if (!pageControlBeingUsed) {
CGFloat pageWidth = self.scrollView.frame.size.width;
pageNumber = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = pageNumber;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
pageNumber1 = pageNumber;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
pageNumber2 = pageNumber;
if(pageNumber == 4 && pageNumber2 == pageNumber1){
//Do any action you want to do from last page or you add by your condition.
}
}
答案 1 :(得分:0)
-(void)scrollPages
{
int currentPage = (int)self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
int totalPages = (int)self.scrollView.contentSize.width / self.scrollView.bounds.size.width;
if(currentPage < totalPages)
{
currentPage++;
}
else
{
currentPage = 1;
}
[self.scrollView setContentOffset:CGPointMake((currentPage-1)*self.scrollView.bounds.size.width,0)];
}
还启用分页。