首页 > 代码库 > iOS UIPageControl与UIScrollView配合使用代码
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]; }
iOS UIPageControl与UIScrollView配合使用代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。