Objective-C: UIKit: UIScrollView、欢迎界面

UIScrollView

#import "MyViewController.h"

@interface MyViewController ()
@property(nonatomic,strong)UIScrollView *sv;
@end

@implementation MyViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    self.sv.contentOffset = CGPointMake(self.sv.contentOffset.x + 100,self.sv.contentOffset.y + 100);
    [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];

    //方式一:关闭 控制器 有导航栏时自动调整滚动视图内容的功能
//    self.automaticallyAdjustsScrollViewInsets = NO;
    //方式二:调整内边距
    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

//scrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

//    NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width + 0.5;
    //round 四舍五入函数
    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)];
    //需要4个圆点
    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);
    //向 scrollView中添加 要显示的内容
    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];
        //将创建好的 imageView 添加到滚动视图上
        [self.scrollView addSubview:imageView];

        //如果是最后一个iamgeView
        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);
            //将button添加到最后一个imageView上
            [imageView addSubview:button];
            //imageView 默认是关闭用户交互的 那么button是imageView的子视图 也跟着关闭了,如果希望button可以点击,就要打开imageView的用户交互
            imageView.userInteractionEnabled = YES;
        }
    }
    //关闭弹跳
    self.scrollView.bounces = NO;

    //设置scrollView整页滑动
    self.scrollView.pagingEnabled = YES;

    //设置水平滚动条不可见
    self.scrollView.showsHorizontalScrollIndicator = NO;

    //将scrollView添加到 self.view 上
    [self.view addSubview:self.scrollView];
}
//Button的 事件方法
-(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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值