#import "MyViewController.h"
@interface MyViewController ()
@property(nonatomic,strong)UIScrollView *sv;
@end
@implementation MyViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.sv setContentOffset:CGPointMake(self.sv.contentOffset.x + 300,self.sv.contentOffset.y + 300) animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
UIScrollView *sv = [[UIScrollView alloc]init];
self.sv = sv;
sv.backgroundColor = [UIColor redColor];
sv.frame = CGRectMake(0, 0, 200, 200);
sv.contentSize = imageView.frame.size;
sv.showsVerticalScrollIndicator = NO;
[sv addSubview:imageView];
sv.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
[self.view addSubview:sv];
}
欢迎界面
#import "MyViewController.h"
@interface MyViewController () <UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageControl;
@end
@implementation MyViewController
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSInteger pageIndex = round(scrollView.contentOffset.x / scrollView.frame.size.width);
self.pageControl.currentPage = pageIndex;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScrollView];
[self setupPageControl];
}
-(void)setupPageControl {
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 40)];
self.pageControl.numberOfPages = 4;
self.pageControl.currentPage = 0;
self.pageControl.pageIndicatorTintColor = [UIColor redColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
self.pageControl.userInteractionEnabled = NO;
[self.view addSubview:self.pageControl];
}
-(void)setupScrollView {
self.scrollView = [[UIScrollView alloc]init];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);
for (NSInteger i = 0; i < 4; i++) {
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
NSString *imageName = [NSString stringWithFormat:@"welcome%ld", i+1];
imageView.image = [UIImage imageNamed:imageName];
[self.scrollView addSubview:imageView];
if (i == 3) {
UIButton *button = [self createButton:@selector(enterLoginVc:)];
button.frame = CGRectMake((imageView.frame.size.width - 150) / 2, (imageView.frame.size.height - 60) / 2, 150, 60);
[imageView addSubview:button];
imageView.userInteractionEnabled = YES;
}
}
self.scrollView.bounces = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:self.scrollView];
}
-(void)enterLoginVc:(UIButton*)sender {
NSLog(@"跳转到 登录界面");
}
-(UIButton*)createButton:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"进入" forState:UIControlStateNormal];
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}