iOS 滑动切换视图

看代码

#import "ViewController.h"
#define kNum 25
#define kAnimationTime 0.3

@interface ViewController ()<UIScrollViewDelegate>
{
    UIView * _chooseView;
    UIScrollView * _scrollView;
    int num;
}
@property (nonatomic, strong)NSTimer * myTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    /*加一个定时器,就可以自动切换视图,就类似平时的广告切换
    dispatch_async(dispatch_get_main_queue(), ^{
        num = 0;
        _myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changView) userInfo:nil repeats:YES];
    });
    */
//    self.navigationController.navigationBar.translucent = NO;
}
- (void)creatView
{
    //创建ScrollView
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNum, self.view.frame.size.width, self.view.frame.size.height-kNum)];
    //设置内部空间
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height-kNum);
    [self.view addSubview:_scrollView];
    _scrollView.pagingEnabled = YES;//开启分页效果
    _scrollView.delegate = self;
    [_scrollView setShowsHorizontalScrollIndicator:NO];//关闭左右活动条
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-kNum);//每页大小
    
    NSArray * buttonTitle = @[@"视图一",@"视图2"];
    for (int i = 0; i < buttonTitle.count; i++) {
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 * i, 18, self.view.frame.size.width/2, 15)];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:buttonTitle[i] forState:UIControlStateNormal];
        
        button.tag = 100 + i;
        
        [button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:button];
    }
    
    for (int i = 0; i < buttonTitle.count; i++) {
        
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i,25, self.view.frame.size.width, self.view.frame.size.height-25)];
        [_scrollView addSubview:view];
        
        CGFloat hue = ( arc4random() % 256 / 256.0 );
        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
        view.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    
    }
    
    _chooseView = [[UIView alloc]initWithFrame:CGRectMake(0, 45 , self.view.frame.size.width/2, 5)];
    _chooseView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:_chooseView];
}

- (void)changView
{
    num++;
    CGPoint page = CGPointMake(num * self.view.frame.size.width, 0);
    [_scrollView setContentOffset:page animated:YES];
    
    if (num == 1) {
        num = -1;
    }

}

#pragma mark - scrollView Deldgate
- (void) scrollViewDidScroll:(UIScrollView *)sender {
    // 得到每页宽度
    CGFloat pageWidth = sender.frame.size.width;
    // 根据当前的x坐标和页宽度计算出当前页数
    int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    CGFloat x =self.view.frame.size.width/2 * currentPage;
    
    [UIView animateWithDuration:kAnimationTime animations:^{
        _chooseView.frame = CGRectMake(x, 45, self.view.frame.size.width/2, 5);
    }];
}

#pragma mark - button Aciton
- (void)chooseAction:(UIButton *)sender
{
     CGFloat x =self.view.frame.size.width/2 * (sender.tag - 100);
    
    [UIView animateWithDuration:kAnimationTime animations:^{
        _chooseView.frame = CGRectMake(x, 45, self.view.frame.size.width/2, 5);
    }];
    
    //计算移动位置
    CGPoint page = CGPointMake((sender.tag - 100)*self.view.frame.size.width, 0);
    [_scrollView setContentOffset:page animated:YES];
}

163820_aJgW_2625446.png163821_PSNa_2625446.png

点击按钮可切换视图,也可左右滑动切换视图。


转载于:https://my.oschina.net/langzhouzhou1/blog/652054

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值