服务器之家

服务器之家 > 正文

iOS基于UIScrollView实现滑动引导页

时间:2021-03-03 15:53     来源/作者:chenzheng8975

上代码前,我们先来看下实现的效果图:

iOS基于UIScrollView实现滑动引导页

WelcomeViewController.h

?
1
2
3
4
5
#import <UIKit/UIKit.h>
 
@interface WelcomeViewController : UIViewController
 
@end

WelcomeViewController.m 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#import "WelcomeViewController.h"
#define IMAGECOUNT 3
 
@interface WelcomeViewController () <UIScrollViewDelegate>
@property (nonatomic, strong)UIPageControl *pageControl;
 
@end
 
@implementation WelcomeViewController
 
- (void)viewDidLoad {
  [super viewDidLoad];
  //创建ScrollView
  UIScrollView *sv = [[UIScrollView alloc] init];
  sv.frame = self.view.bounds;
  //设置边缘不弹跳
  sv.bounces = NO;
  //整页滚动
  sv.pagingEnabled = YES;
  sv.showsHorizontalScrollIndicator = NO;
   
  //加入多个子视图(ImageView)
  for(NSInteger i=0; i<IMAGECOUNT; i++){
    NSString *imgName = [NSString stringWithFormat:@"%ld", i+1];
    UIImage *image = [UIImage imageNamed:imgName];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    CGRect frame = CGRectZero;
    frame.origin.x = i * sv.frame.size.width;
    frame.size = sv.frame.size;
    imageView.frame = frame;
    [sv addSubview:imageView];
     
    if(i==IMAGECOUNT-1){
      //开启图片的用户点击功能
      imageView.userInteractionEnabled = YES;
      //加个按钮
      UIButton *button = [[UIButton alloc]init];
       
      button.frame = CGRectMake((imageView.frame.size.width-150)/2, imageView.frame.size.height*0.8, 150, 40);
      button.backgroundColor = [UIColor orangeColor];
      [button setTitle:@"立即体验" forState:UIControlStateNormal];
      button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
      [imageView addSubview:button];
      [button addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];    }
  }
   
  sv.contentSize = CGSizeMake(IMAGECOUNT * sv.frame.size.width, sv.frame.size.height);
   
  [self.view addSubview:sv];
   
  //加入页面指示控件PageControl
  UIPageControl *pageControl = [[UIPageControl alloc]init];
  self.pageControl = pageControl;
  //设置frame
  pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20);
  //分页面的数量
  pageControl.numberOfPages = IMAGECOUNT;
  //设置小圆点渲染颜色
  pageControl.pageIndicatorTintColor = [UIColor whiteColor];
  //设置当前选中小圆点的渲染颜色
  pageControl.currentPageIndicatorTintColor = [UIColor redColor];
  //关闭用户点击交互
  pageControl.userInteractionEnabled = NO;
   
  [self.view addSubview:pageControl];
   
  sv.delegate = self;
   
   
}
- (void)enter
{
  NSLog(@"进入应用");
}
 
//UIScrollViewDelegate方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  CGPoint offset = scrollView.contentOffset;
  if(offset.x<=0){
    offset.x = 0;
    scrollView.contentOffset = offset;
  }
  NSUInteger index = round(offset.x / scrollView.frame.size.width);
  self.pageControl.currentPage = index;
}
 
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
 
 
@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部