我正在尝试制作一个练习应用程序,在其中可以使用页面控件滚动图像。我能够滚动图像并包含页面控制,但我遇到的问题是我无法将它们联系起来。换句话说,当我滚动图像时,页面控制不会受到影响,当我更改页面控制时,图像的滚动也不受影响。
我参考了这个页面:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/,用于实现带有页面控制的滚动。
Viewcontroller.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIScrollViewDelegate>{
UIScrollView *scrollView;
UIPageControl *pageControl;
BOOL pageControlBeingUsed;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage;
@end
Viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize scrollView,pageControl;
- (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;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg" ], nil];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView* imgView = [[UIImageView alloc] init];
imgView.image = [images objectAtIndex:i];
imgView.frame = frame;
[scrollView addSubview:imgView];
}
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = images.count;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
- (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];
pageControlBeingUsed = YES;
}
@end
需要一些关于这个的指导...谢谢。