iOS UIPageControl与UIScrollView配合使用代码
#import "RootViewController.h" #import "RootView.h" @interface RootViewController ()<UIScrollViewDelegate> @property(nonatomic,retain)RootView *myview; @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.myview = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.myview.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; } return self; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { self.myview.page.currentPage = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width; } -(void)pageAction:(UIPageControl*)sender { [UIView animateWithDuration:0.5 animations:^{ self.myview.scrollV.contentOffset = CGPointMake(sender.currentPage*[UIScreen mainScreen].bounds.size.width, 0); }]; } -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.myview.imV; } -(void)loadView { self.view = self.myview; self.myview.scrollV.delegate = self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
#import <UIKit/UIKit.h> @interface RootView : UIView @property(nonatomic,retain)UIScrollView *scrollV; @property(nonatomic,retain)UIImageView *imV; @property(nonatomic,retain)UIPageControl *page; @end
#import "RootView.h" @implementation RootView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addAllViews]; } return self; } -(void)addAllViews { self.scrollV = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.scrollV.backgroundColor = [UIColor yellowColor]; self.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*8, 0); self.scrollV.pagingEnabled = YES; // self.scrollV.bounces = NO; // self.scrollV.bouncesZoom = NO; self.scrollV.minimumZoomScale = 0.3; self.scrollV.maximumZoomScale = 2; self.scrollV.zoomScale = 0.3; [self addSubview:self.scrollV]; for (int i = 0; i < 9; i++) { self.imV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"h%d.jpeg",i+1]]]; self.imV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); [self.scrollV addSubview:self.imV]; } self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-30, [UIScreen mainScreen].bounds.size.width, 30)]; self.page.backgroundColor = [UIColor grayColor]; self.page.numberOfPages = 8; self.page.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; self.page.pageIndicatorTintColor = [UIColor grayColor]; [self addSubview:self.page]; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。