我有 3 个页面,它们都包含 youtube 视频( WebView ),这些 WebView 在 ScrollView 中。我使用了一个教程来做到这一点,在那个教程中,每次你向任一方向滑动时它都会停在下一页(就像在 ios 主屏幕中一样)但是我的只是一直移动然后停止取决于滑动 Action 的时间。
这是我在 .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 主屏幕)上停止?
最佳答案
你有没有设置pagingEnabled将 UIScrollView 设置为 YES。
- (void)viewDidLoad {
[super viewDidLoad];
// EXISTING CODE
self.scrollView.pagingEnabled = YES;
}
关于ios - 使 UIPageControl 在每个页面上停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15601538/