ScrollView&PageControl

 1 //
 2 //  ViewController.m
 3 //  分屏导航
 4 //
 5 //  Created by wky on 15/10/2017.
 6 //  Copyright © 2017 vector. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
13 @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
14 
15 
16 - (IBAction)changePage:(id)sender;
17 
18 @property (strong,nonatomic) UIImageView* imageView1;
19 @property (strong,nonatomic) UIImageView* imageView2;
20 @property (strong,nonatomic) UIImageView* imageView3;
21 
22 @end
23 
24 @implementation ViewController
25 
26 - (void)viewDidLoad {
27     [super viewDidLoad];
28     // Do any additional setup after loading the view, typically from a nib.
29     
30     self.scrollView.delegate = self;
31     
32     self.scrollView.frame = self.view.frame;
33     
34     CGFloat width = self.view.frame.size.width;
35     
36     CGFloat height = self.view.frame.size.height;
37     
38     self.scrollView.contentSize = CGSizeMake(width*3,height);
39     
40     
41     //1
42     self.imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,480)];
43     
44     self.imageView1.image = [UIImage imageNamed:@"1.png"];
45     
46     [self.scrollView addSubview:self.imageView1];
47     
48     //2
49     self.imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(320,0,320,480)];
50     
51     self.imageView2.image = [UIImage imageNamed:@"2.png"];
52     
53     [self.scrollView addSubview:self.imageView2];
54 
55     //3
56     self.imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(320*2,0,320,480)];
57     
58     self.imageView3.image = [UIImage imageNamed:@"3.png"];
59     
60     [self.scrollView addSubview:self.imageView3];
61     
62    
63 
64     
65     
66 }
67 //在头文件中继承一下UIScrollViewDelegate
68 -(void) scrollViewDidScroll:(UIScrollView *)scrollView
69 {
70     CGPoint offset = scrollView.contentOffset;
71     
72     self.pageControl.currentPage= offset.x/320;
73     
74 }
75 
76 
77 
78 
79 - (void)didReceiveMemoryWarning {
80     [super didReceiveMemoryWarning];
81     // Dispose of any resources that can be recreated.
82 }
83 
84 
85 - (IBAction)changePage:(id)sender {
86     
87     [UIView animateWithDuration:0.3f animations:^{
88         NSInteger whichPage = self.pageControl.currentPage;
89         self.scrollView.contentOffset = CGPointMake(320*whichPage, 0);
90     }];
91     
92     
93 }
94 @end

 

posted @ 2017-10-15 21:58  vector11248  阅读(158)  评论(0编辑  收藏  举报