使UIPageControl在每个页面上停止

时间:2013-03-24 17:18:34

标签: ios xcode uiwebview uiscrollview uipagecontrol

我有3个页面都包含youtube视频(网页浏览)这些网页浏览在滚动视图中。我用了一个教程来做这个,在那个教程中,每次你向任一个方向滑动它都会停在下一页(就像在ios主屏幕上一样)但是我只是继续移动然后停止,这取决于滑动动作的时间长短。

这是我在.m中的代码:

#import "FeaturedVideosViewController.h"

@interface FeaturedVideosViewController ()

@end

@implementation FeaturedVideosViewController

@synthesize scrollView, pageControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}



- (void)viewDidLoad {
[super viewDidLoad];

pageControlBeingUsed = NO;
{
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * 3;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
        [self.scrollView addSubview:subview];

}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 3, self.scrollView.frame.size.height);

self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = 3;
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/embed/wYkIhYtN7yA"]]];
[WebView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/embed/4O7kTOTg1UE"]]];
[WebView3 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/embed/vmUW41m93oY"]]];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}

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

- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];

// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}




@end

我如何让它在每个页面上停止,例如ios主屏幕?

2 个答案:

答案 0 :(得分:2)

您是否已将UIScrollView的pagingEnabled设置为YES。

- (void)viewDidLoad {
    [super viewDidLoad];
    // EXISTING CODE
    self.scrollView.pagingEnabled = YES;
  }

答案 1 :(得分:0)

单击UIScrollView,然后单击右侧(Attribute Inspector),勾选Paging Enabled。 要么 您也可以将其设置为programaticaly

scrollView.pagingEnabled = yes;

就是这样......